Array issues

Hey, I keep having a hard time getting setting a position in an array to an object;

Private Object[] Squad;

    void OnMouseDown()
    {
        for (int men = 1; men < 13; men ++)
        {
            Debug.Log (SpawnSoldier ());
            Squad[(int)men] = SpawnSoldier ();
        }

    }

    Object SpawnSoldier()
    {
        Object soldier = Instantiate (Resources.Load("soldier"), this.transform.position + new Vector3(0,0,2), new Quaternion(0,0,0,0) );
        return soldier;
    }

It keeps giving me a NullReferenceException. Debug.Log(SpawnSoldier()) tells me it is even an object. What am I doing wrong?

Theres no object called “soldier” for resources to Load

ps. arrays start at index 0

No, it is a resource. I have that much working. It creates it without an array or for loop, but throws an error at me, if I do try to use an array or for loop regardless.

Use the correct type, rather than Object. Since your loop iterator is already an int, don’t cast to int. Just Squad[men]. And yes, start at 0, not 1.

–Eric

Yeah, I know about 0. I just set it to 1 to see if it would fix the problem. Was a shot in the dark I admit.

I changed the Object[ ] squad to GameObject[ ] squad but I still have issues because it traces back to before with Resources.Load(“soldier”) as GameObject in SpawnSoldier();

Object SpawnSoldier()
    {
        GameObject soldier = Instantiate (Resources.Load("soldier") as GameObject, this.transform.position + new Vector3(0,0,2), new Quaternion(0,0,0,0) );
        return soldier;
    }

It gives me an error of; Cannot implicitly convert type UnityEngine.Object to UnityEngine.GameObject . Any ideas?

Soldier is a prefab by the way if you are wondering.

You’re not casting Instantiate to the correct type, so it’s just returning Object.

–Eric

How do I fix that then? I really apologize for being so unexperienced with Unity and such, thus taking up so much of your time.

GameObject soldier = (GameObject) Instantiate (...)

I see. I am so sorry everyone for making such a small mistake. It still isn’t working though. Now I have;

private GameObject[] Squad;

    void OnMouseDown()
    {
        for (int men = 0; men < 13; men ++)
        {
            Debug.Log (SpawnSoldier ());
            Squad[men] = SpawnSoldier ();
        }

    }

    GameObject SpawnSoldier()
    {
        GameObject soldier = (GameObject) Instantiate (Resources.Load("soldier"), this.transform.position + new Vector3(0,0,2), new Quaternion(0,0,0,0) );
        return soldier;
    }

Use public variables with drag’n’drop instead of Resources.Load. Less fiddly, avoids strings, easier and more flexible asset management.

public GameObject soldierPrefab;
...
...Instantiate (soldierPrefab...

–Eric

1 Like

None of what has been told to me has worked. I think at this point if I just upload the project files someone would be able to work it out better.

Dropbox - Error - Simplify your life Please, tell me what I should have as the code.

Also, if I change it to public GameObject[ ] Squad I get Array Index out of bounds for the for loop.

Are you initialising your array anywhere?

I have

public GameObject[] Squad;

Why is it public? Did you initialize it in the inspector?

–Eric

In public or private I still get errors. Even when I set the size in the inspector.

I also know that it is specific to the array, as without trying to assign values to indexes in the array within the for loop, it runs fine and does not throw me an error.

I even tried something to test where the issue was; it is exactly with the array. The program runs perfectly fine with the below code;

public GameObject Man1;
    public GameObject Man2;

    void OnMouseDown()
    {
        Man1 = SpawnSoldier ();
        Man2 = SpawnSoldier ();
        /*for (int men = 0; men < 13; men ++)
        {
            GameObject new_soldier = SpawnSoldier ();
            Squad[men] = new_soldier;
        }*/

    }

    GameObject SpawnSoldier()
    {
        return (GameObject) Instantiate (Resources.Load("soldier"), this.transform.position + new Vector3(0,0,2), new Quaternion(0,0,0,0) ) as GameObject;
    }

Also, it would be better to create a variable and only call Resources.Load(“soldier”) once, set the variable equal to it, then make your SpawnSoldier function instantiate the variable instead of instantiating (Resources.load), cause Resources.Load is relatively slow/expensive and its better to create references first.