get_isPlaying is not allowed to be called from a MonoBehaviour constructor

kemikalx61 3 years ago updated by Lazlo Bonin (Lead Developer) 3 years ago 1

Good Day

Recently, I begun getting the following error "

get_isPlaying is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'ObjectSpawn'.
See "Script Serialization" page in the Unity Manual for further details.
Chronos.Singleton`1:get_instance() (at Assets/Chronos/Source/Utilities/Singleton.cs:25)
ObjectSpawn:.ctor() (at Assets/_Scripts/ObjectSpawn.cs:18)

" I  was playing around with a script that was 100% fine before and it started showing up. Despite the errors being seemingly detailed. I am not able to find out why it's occurring as it doesnt break / stop the game but shows up and id like to handle it. 

It points to the following line with isPlaying.

public static T instance
if (!Application.isPlaying)
T[] instances = FindObjectsOfType<T>();

And please find the entirely of my Object Spawn code below

using UnityEngine;
using System.Collections;
using Chronos;

public class ObjectSpawn : BaseBehaviour //MonoBehaviour

    public float minTime = 3f;
    public float maxTime = 9f;
    public float minX = -65.5f;
    public float maxX = -5.5f;
    public float topY = -5.5f;
    public float z = 0.0f;
    public int count = 50;
    public GameObject prefab;
    public bool doSpawn = true;

    Clock clock = Timekeeper.instance.Clock("MovingOneWayPlatforms");
    void Start()

    IEnumerator Spawner()
        while (doSpawn   && count > 0  )

            Renderer renderer = GetComponent<Renderer>();
            float min = renderer.bounds.min.x;
            float max = renderer.bounds.max.x;
            Vector3 v12 = new Vector3(Random.Range(minX, maxX), this.gameObject.transform.position.y, 0f);
            prefab.GetComponent<Rigidbody2D>().gravityScale =1.0f;
            prefab =  Instantiate(prefab, v12, Random.rotation);
           //  yield return new WaitForSeconds(Random.Range(minTime, maxTime)); 
              yield return time.WaitForSeconds(Random.Range(minTime, maxTime));
             Destroy(prefab, 6);


Chronos Version:
Unity Version:

Hi there!

As answered on the discord, the issue is that you shouldn't use Unity or Chronos API at the time of class construction.

The problem here is this line:

Clock clock = Timekeeper.instance.Clock("MovingOneWayPlatforms");

You should move it to Start, and your problem will be gone!