Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Peterglobe, Dec 11, 2019.

  1. Peterglobe

    Peterglobe

    Joined:
    Nov 17, 2019
    Posts:
    2
    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!
    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public class ObstacleSpawn : MonoBehaviour
    4. {
    5.  
    6.     /* float timeToObstacle;
    7.      float blinkTime = 0.5f;
    8.      float toObstacleMin = 5.1f;
    9.      float toObstacleMax = 60.1f;
    10.  
    11.  
    12.      public GameObject explosion;
    13.      public GameObject circle;
    14.      public GameObject player;
    15.  
    16.      Vector3 circlePos;
    17.      float circleX;
    18.      float circleZ;
    19.  
    20.      float obstacleRad;
    21.      float radMin;
    22.      float radMax;
    23.  
    24.      private System.Random rnd;
    25.  
    26.      public Text Log;
    27.  
    28.  
    29.      // Start is called before the first frame update
    30.      void Start()
    31.      {
    32.          rnd = new System.Random();
    33.  
    34.          timeToObstacle = rnd.Next((int)toObstacleMin, (int)toObstacleMax);
    35.  
    36.          radMin = 2f;
    37.          radMax = 20f;
    38.  
    39.          obstacleRad = rnd.Next((int)radMin, (int)radMax);
    40.  
    41.          circlePos = new Vector3(UnityEngine.Random.Range(-13.8f, 9.4f), 3.4f, UnityEngine.Random.Range(-8.5f, 13.6f));
    42.      }
    43.  
    44.      // Update is called once per frame
    45.      void Update()
    46.      {
    47.          timeToObstacle -= Time.deltaTime;
    48.  
    49.          if (timeToObstacle <= 3f)
    50.          {
    51.              Log.text = "Obstacle spawned!";
    52.  
    53.              blinkTime -= Time.deltaTime;
    54.  
    55.              Instantiate(circle, circlePos, Quaternion.identity);
    56.              circle.transform.localScale += obstacleRad;
    57.  
    58.              if (blinkTime <= 0f)
    59.              {
    60.                  circle.SetActive(false);
    61.  
    62.                  if(blinkTime <= -0.5f)
    63.                  {
    64.                      circle.SetActive(true);
    65.  
    66.                      blinkTime = 0.5f;
    67.                  }
    68.              }
    69.  
    70.              if(timeToObstacle <= 0f)
    71.              {
    72.                  Instantiate(explosion, circlePos, Quaternion.identity);
    73.  
    74.                 if(Vector3.Distance(circlePos, GameObject.Find("Player").transform.position) <= obstacleRad){
    75.  
    76.                      SceneManager.LoadScene(0);
    77.                  }
    78.                  else
    79.                  {
    80.                      obstacleRad = rnd.Next((int)radMin, (int)radMax);
    81.                      circlePos = new Vector3(UnityEngine.Random.Range(-13.8f, 9.4f), 3.4f, UnityEngine.Random.Range(-8.5f, 13.6f));
    82.                      timeToObstacle = rnd.Next((int)toObstacleMin, (int)toObstacleMax);
    83.                      Destroy(circle);
    84.                      blinkTime = 0.5f;
    85.  
    86.                      Log.text = "Obstacle survived!";
    87.                  }
    88.  
    89.              }
    90.          }
    91.      } */
    92.  
    93.     int amountObstacles;
    94.     float timeToObstacle;
    95.  
    96.     public System.Random rnd;
    97.  
    98.     public GameObject obstacle;
    99.  
    100.     public Text Log;
    101.  
    102.     void Start()
    103.     {
    104.         Debug.Log(1);
    105.         amountObstacles = rnd.Next(1, 20);
    106.         timeToObstacle = UnityEngine.Random.Range(10.0f, 60.0f);
    107.         Log.text = timeToObstacle + "s left to Wave";
    108.         Debug.Log(2);
    109.     }
    110.  
    111.     void Update()
    112.     {
    113.         timeToObstacle -= Time.deltaTime;
    114.  
    115.         Log.text = timeToObstacle + "s left to Wave";
    116.  
    117.         Debug.Log(timeToObstacle + "s left to Wave");
    118.  
    119.         if (timeToObstacle <= 0f)
    120.         {
    121.             for (int i = 0; i < amountObstacles; i++)
    122.             {
    123.               Vector3 _pos = new Vector3(UnityEngine.Random.Range(-13.8f, 9.4f), 3.4f, UnityEngine.Random.Range(-8.5f, 13.6f));
    124.               Instantiate(obstacle, _pos, Quaternion.identity);
    125.             }
    126.  
    127.             Log.text = amountObstacles + " Obstacles spawned";
    128.  
    129.             amountObstacles = rnd.Next(1, 20);
    130.  
    131.             timeToObstacle = UnityEngine.Random.Range(10.0f, 60.0f);
    132.  
    133.         }
    134.     }
    135.  
    136. }
    137.  
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Is that game object and behaviour active and enabled?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Checklist for when your code doesn't seem to run:
    1. Check the Unity console for errors
    2. Check that your script is attached to an object in your scene
    3. Check that that object is active (in hierarchy)
    4. Check that your script is enabled
    5. 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")
    6. Check that you actually clicked "save" in the editor after the last time you modified the code
    7. Click "Play", then repeat steps #1-4
     
    Kurt-Dekker likes this.