Adding a GameObject to a list and it won't get added

I’m having this problem where I have a game object (a prefab that is in the Hierarchy) but when I add it to the list it isn’t added. Well I can’t check because Debug.Log(theList) isn’t giving me anything in my console either, And I know the script is loaded because it works for other things.

This is the script I use for creating the list, and the GameObject

    private List<GameObject> weapons = new List <GameObject>();
    public List<GameObject> Weapons { get { return weapons; } }
    private GameObject chosenPrimary;
    public GameObject ChosenPrimary {  get { return chosenPrimary; } }

and this is what I use for adding it to the list

    void Start()
    {
        chosenPrimary = GameObject.Find("PrimaryPlaceholder");
        UnityEngine.Debug.Log(chosenPrimary);
        weapons.Add(chosenPrimary);
        UnityEngine.Debug.Log(weapons);
    }

Neither of the Debug.Log lines are giving anything to the console.
Sorry if it was messy I’m new to the forum.

Oh and I know it’s not being added because in another script I call for Weapons[0] and it gives this error

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)

The first thing to fix is this sort of silliness:

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

More information: https://discussions.unity.com/t/899843/12

In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.
If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.

“Stop playing ‘Where’s GameWaldo’ and drag it in already!”

After that, don’t just print the list, print how many items it has… iterate and print each of the items, etc.

This makes it sound like the code isn’t even running. Find out why. Fix that first.

Make sure your log console selector buttons are enabled. See this graphic:

https://discussions.unity.com/t/733002/10

https://discussions.unity.com/t/804947/4

And finally… debugging…

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

1 Like

I’m using GameObject.Find because the PrimaryPlaceholder gameObject is going to be deleted and the only way to get the type of object I’m looking for is to use it, but for now I’m just trying to get it so the mechanics of weapons are working. I managed to fix my issue and I was marking it fixed the moment you posted your helpful reply. The code was defenitely running because it worked with other stuff in the game aswell.

Thank you for your help on another one of my dumb questions.

No, not at all. You should read the links I already provided above. There’s a billion better ways to do it.

Using GameObject.Find() is like a contractor who comes to put a new roof on your house and says that he’s gonna use some old plywood he found at the dump to save you a few dollars. GameObject.Find() is never a good idea.

At what point in time does the other script try to access Weapons[0]? Is it possible that this access happens before Start() runs on the script you showed in the original post?

You never need to use those methods, and you generally shouldn’t because they’re slow and only become slower with the more objects you have in the scene. If you’re using a prefab and the reference is between objects in the prefab you can link them up in the Inspector. If you’re using Instantiate you can have code link the reference returned by it.