Script doesn't start to run, but it's pretty simple...

Please help me searching the mistake I’ve made, follwing script doesn’t start to run. It on a gameobject but thre is nothing. Debug.Log should show me how far it comes, but I haven’t received on Debug.Log yet!

using UnityEngine.UI;

public class ObstacleSpawn : MonoBehaviour
{

    /* float timeToObstacle;
     float blinkTime = 0.5f;
     float toObstacleMin = 5.1f;
     float toObstacleMax = 60.1f;


     public GameObject explosion;
     public GameObject circle;
     public GameObject player;

     Vector3 circlePos;
     float circleX;
     float circleZ;

     float obstacleRad;
     float radMin;
     float radMax;

     private System.Random rnd;

     public Text Log;


     // Start is called before the first frame update
     void Start()
     {
         rnd = new System.Random();

         timeToObstacle = rnd.Next((int)toObstacleMin, (int)toObstacleMax);

         radMin = 2f;
         radMax = 20f;

         obstacleRad = rnd.Next((int)radMin, (int)radMax);

         circlePos = new Vector3(UnityEngine.Random.Range(-13.8f, 9.4f), 3.4f, UnityEngine.Random.Range(-8.5f, 13.6f));
     }

     // Update is called once per frame
     void Update()
     {
         timeToObstacle -= Time.deltaTime;

         if (timeToObstacle <= 3f)
         {
             Log.text = "Obstacle spawned!";

             blinkTime -= Time.deltaTime;

             Instantiate(circle, circlePos, Quaternion.identity);
             circle.transform.localScale += obstacleRad;

             if (blinkTime <= 0f)
             {
                 circle.SetActive(false);

                 if(blinkTime <= -0.5f)
                 {
                     circle.SetActive(true);

                     blinkTime = 0.5f;
                 }
             }

             if(timeToObstacle <= 0f)
             {
                 Instantiate(explosion, circlePos, Quaternion.identity);

                if(Vector3.Distance(circlePos, GameObject.Find("Player").transform.position) <= obstacleRad){

                     SceneManager.LoadScene(0);
                 }
                 else
                 {
                     obstacleRad = rnd.Next((int)radMin, (int)radMax);
                     circlePos = new Vector3(UnityEngine.Random.Range(-13.8f, 9.4f), 3.4f, UnityEngine.Random.Range(-8.5f, 13.6f));
                     timeToObstacle = rnd.Next((int)toObstacleMin, (int)toObstacleMax);
                     Destroy(circle);
                     blinkTime = 0.5f;

                     Log.text = "Obstacle survived!";
                 }

             }
         }
     } */

    int amountObstacles;
    float timeToObstacle;

    public System.Random rnd;

    public GameObject obstacle;

    public Text Log;

    void Start()
    {
        Debug.Log(1);
        amountObstacles = rnd.Next(1, 20);
        timeToObstacle = UnityEngine.Random.Range(10.0f, 60.0f);
        Log.text = timeToObstacle + "s left to Wave";
        Debug.Log(2);
    }

    void Update()
    {
        timeToObstacle -= Time.deltaTime;

        Log.text = timeToObstacle + "s left to Wave";

        Debug.Log(timeToObstacle + "s left to Wave");

        if (timeToObstacle <= 0f)
        {
            for (int i = 0; i < amountObstacles; i++)
            {
              Vector3 _pos = new Vector3(UnityEngine.Random.Range(-13.8f, 9.4f), 3.4f, UnityEngine.Random.Range(-8.5f, 13.6f));
              Instantiate(obstacle, _pos, Quaternion.identity);
            }

            Log.text = amountObstacles + " Obstacles spawned";

            amountObstacles = rnd.Next(1, 20);

            timeToObstacle = UnityEngine.Random.Range(10.0f, 60.0f);

        }
    }

}

Is that game object and behaviour active and enabled?

The above script does not compile due to missing a using UnityEngine; directive.

I see you including the UnityEngine.UI space, but that explicitly does not include the parent space, which is where things like GameObject, MonoBehavior and more are defined.

Checklist for when your code doesn’t seem to run:

  • Check the Unity console for errors
  • Check that your script is attached to an object in your scene
  • Check that that object is active (in hierarchy)
  • Check that your script is enabled
  • Check that you haven’t hidden the error messages in the Unity console using any of those filter buttons at the top of the console (including “collapse”)
  • Check that you actually clicked “save” in the editor after the last time you modified the code
  • Click “Play”, then repeat steps #1-4
1 Like