Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question GameObjects randomly turn null

Discussion in 'Scripting' started by MrFacysus, Jul 26, 2023.

  1. MrFacysus

    MrFacysus

    Joined:
    Aug 30, 2020
    Posts:
    4
    Basically, I am trying to make a tower defense game, and to optimize its performance I wanted to predefine and search for each object before running any loops so the loops had an easy time running and didn't require much search for objects.

    My Start() code:

    Code (CSharp):
    1. private GameObject enemyList;
    2.     private GameObject tower;
    3.     private GameObject barrel;
    4.     private GameObject range;
    5.     private int i = 0;
    6.     private int timeElapsed = 0;
    7.  
    8.     void Start()
    9.     {
    10.         tower = this.gameObject;
    11.         while (enemyList == null)
    12.             enemyList = transform.parent.Find("Map").Find("EnemySpawner").gameObject;
    13.         barrel = tower.transform.GetChild(0).GetChild(0).gameObject;
    14.         range = tower.transform.GetChild(1).gameObject;
    15.     }
    and also my Update() which causes errors since enemyList goes null every 2nd run
    Code (CSharp):
    1. void Update()
    2.     {
    3.         Debug.Log(enemyList);
    4.         Debug.Log(enemyList.transform);
    5.         for (i = 0; i < enemyList.transform.childCount; i++)
    6.         {
    7.             if (Vector3.Distance(enemyList.transform.GetChild(i).transform.position, tower.transform.position) < 5)
    8.             {
    9.                 Debug.LogWarning("looked");
    10.                 barrel.transform.LookAt(enemyList.transform.GetChild(i).transform);
    11.                 break;
    12.             }
    13.         }
     
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    This assumes you know about the C# (or Java and so on) reference system: in Unity when GameObjects are destroyed (you run the Unity "Destroy" command on them, maybe when they're blown up by the enemy; which removes them from the Scene), all pointers to them magically become null.

    Of course, this is impossible, which is what may be confusing you. Of course the only way to make something be null is to change it to null. But Unity uses a hack -- "==null" is overloaded and returns true on null or if a GameObject's secret "destroyed" flag has been set. It seems crazy but it saves new coders from needing "if(enemyP==null || enemyP.isDestroyed)". And of course it works with all Unity objects -- Transforms, Scripts ... .
     
    MrFacysus likes this.
  3. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    435
    I know it's test code, but using a while loop in the way above is a recipe for a never-ending loop at some point. If you know all elements from the scene at the beginning you don't need to search them, you connect them in Inspector by references. There can be also a handy SceneManager that tracks what's going on.
     
    MrFacysus likes this.
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    If you tag all your spawners with "EnemySpawner" then the weird lines 11 and 12 can be replaced with something like this:

    Code (CSharp):
    1. enemyList = GameObject.FindGameObjectsWithTag("EnemySpawner");


    Or if they have a script named EnemySpawner and you don't want to tag them then you can use this:

    Code (CSharp):
    1. EnemySpawner[] enemyList;
    2. enemyList = FindObjectsOfType<EnemySpawner>();
     
    MrFacysus likes this.
  5. MrFacysus

    MrFacysus

    Joined:
    Aug 30, 2020
    Posts:
    4
    This helps a lot thank you!
     
  6. MrFacysus

    MrFacysus

    Joined:
    Aug 30, 2020
    Posts:
    4
    Also true, thank you for that!
     
  7. MrFacysus

    MrFacysus

    Joined:
    Aug 30, 2020
    Posts:
    4
    Yea the error was due to the lack of attention uhh I had a misplaced tower sitting on the menu with the script which caused it due to my way of finding the spawners with .parent.parent type of code!