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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Adding a GameObject to a <List> of objects

Discussion in 'Scripting' started by bian101, Dec 16, 2015.

  1. bian101

    bian101

    Joined:
    Nov 28, 2015
    Posts:
    4
    Hello,

    So I am pretty new to Unity and learning as I go. I have a lot of C++ game development experience using a very protracted engine, so moving to Unity is quite fresh!

    What I am trying to do:

    • Add a new aircraft to the game
    • Push the instanciated aircraft onto a list
    • Access all instanciated aircrafts to run their methods which makes the AI work

    The Code:

    public class GameManager : MonoBehaviour {​

    // store a list of aircraft
    List<GameObject> aircrafts;

    void Start () {

    // spawn!

    // load an aircraft prefab as a GameObject initially
    gameobj = (GameObject)Instantiate(Resources.Load("Aircraft"));

    // give the gameobj the class Aircraft to access its member functions
    Aircraft aircraft = gameobj.AddComponent<Aircraft>();
    m_aircrafts.Add(aircraft); // push to the List

    int i = 0;

    foreach (GameObject ac in aircrafts)
    {
    Debug.Log("Hello, you are: " + i);
    i++;​
    }​
    }​
    }​

    However the line `aircrafts.Add(aircraft);` gives the error: "object instance not set to an instance of an object". From Google it looks like the object aircraft doesn't exist? I'm not entirely sure why it wont push the aircraft to the list.
     
    Last edited: Dec 17, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    you haven't "created the list", all you've said is "I'm going to have a list called dave".

    you need
    Code (csharp):
    1.  
    2. aircrafts = new List<GameObject>();
    3.  
    somewhere before you try to do anything with it.


    close, the list "aircrafts" doesn't exist so you can't add anything to it yet. Null exception errors are usually complaining about the object before the "." rather than the thing after. You can add null to a list quite happily, you can't add something to a null list.
     
    bian101 likes this.
  3. bian101

    bian101

    Joined:
    Nov 28, 2015
    Posts:
    4
    Thanks for the reply! Kicking myself for missing that one, thank you!
     
  4. simtheythem

    simtheythem

    Joined:
    Aug 7, 2022
    Posts:
    1
    Oh. My. GOODNESS
    I've been going through so many threads looking for some clever quirk or nuance to Unity I don't know about, reorganized my code every which way scratching a new hole in my head for over two hours and THIS was the problem?!

    *head-desk*

    Thank you in 2022, LeftyRighty!