Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Array of serialized prefabs is coming back NULL, unable to instantiate (Null reference)

Discussion in 'Scripting' started by fairbrasssamuel, May 26, 2024.

  1. fairbrasssamuel

    fairbrasssamuel

    Joined:
    May 26, 2024
    Posts:
    3
    Hi, I am using the Runner Template in Unity to learn and get some experience, and in my project I want to randomly spawn one of the included prefabs, a coin, at runtime. I have created a serialised array of GameObjects (as this will eventually be random items), but I cannot instantiate the prefab at all.

    When writing the Debug.Log I can see the array is NULL. I cannot perform the .length command on the array either (which I would have assumed would work if the prefab had an issue, since it would either be an empty array or an array with the buggy prefab. I assume the prefab works as it is included in the template and I can manually drag it into the game and interact with it perfectly well. If I remove the null check, this is the error I get:

    `NullReferenceException: Object reference not set to an instance of an object`

    I am completely out of ideas. One other thought I had was maybe it is not 'available' during when I test the scene or level, but again surely that would be an empty array? How can it be null if it is serialised and I have selected the asset from the inspector? This is my code:

    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2.     {
    3.         ...
    4.  
    5.         [SerializeField] public GameObject[] prefabs;
    6.  
    7.         ...
    8.         public void SpawnRandomObject() {
    9.             Debug.Log(prefabs);
    10.  
    11.             if (prefabs != null) {
    12.                 Vector3 position = new Vector3(UnityEngine.Random.Range(1f, 1f), 1, UnityEngine.Random.Range(1f, 1f));
    13.                 int x = UnityEngine.Random.Range(0, prefabs.Length);
    14.                 Instantiate(prefabs[x], position, Quaternion.identity);
    15.             }
    16.     }
    17.     ...
    18. }
    Something so simple like this has me pulling my hair out!! Any response will be greatly appreciated, thank you!
     
    Last edited: May 26, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Is GameManager on a prefab GameObject and the prefabs list contains references to in-scene objects? If so, these will not be valid because a prefab cannot reference scene objects. But the list itself shouldn't be null though.

    Please detail what game object GameManager is on, whether this is a prefab, whether there may be more than one GameManager, and what these "prefabs" are.

    Generally speaking, avoid using overly generic names. A list of "prefabs" is as non-descriptive as it can be, particularly in a class that whose name indicates "the entirety of the project itself and it manages everything within it".
    At the very least name that list "coinPrefabs" if it spawns coins. ;)
     
    Bunny83 likes this.
  3. fairbrasssamuel

    fairbrasssamuel

    Joined:
    May 26, 2024
    Posts:
    3
    Yes GameManager is a prefab asset included in the scene for the level. The coin prefab I took from "Assets", although I did also try spawning a coin into the scene and then using that coin as the object... One oddity perhaps is that I am using the PlayerController to create a GameManager instance in order to manipulate the world (which I have done by being able to spawn meshes and add materials to them, using adapted template code after the player has moved x distance, but I have a feeling this is just wrong!). It is in that game instance where I call this method, but the other one which adds objects works. Perhaps it is related to that?

    And thank you haha, I have renamed the array!

    [Edit] Interestingly just seen that that array is NOT empty when calling it from the player controller. I wonder if it is because I instantiate a new GameManager ?
     
  4. fairbrasssamuel

    fairbrasssamuel

    Joined:
    May 26, 2024
    Posts:
    3
    Fixed... passed down an object from PlayerController to the method and it works! Clearly I need to make some kind of higher level controller and control this sort of stuff.

    Thanks for the input @CodeSmile , would never have considered something like that :D
     
    Bunny83 and CodeSmile like this.