2
\$\begingroup\$

Sorry if this sounds quite primitive, I am new to Unity. I am trying to instantiate an array of prefabs, but it doesn't seem to appear when I run the game. Firstly I created my block prefabs, then I created an empty game object and added the following script to the empty object.

public class BlocksTestScript : MonoBehaviour {

    public GameObject[] blockPrefabs;
    // Use this for initialization
    void Start () {

        blockPrefabs = new GameObject[7];
        blockPrefabs = Resources.LoadAll ("/Prefab/BlockPrefabs") as GameObject[];

                for (int i = 0; i < blockPrefabs.Length; i++) {
        GameObject myPrefabs = Instantiate(blockPrefabs[i], blockPrefabs[i].transform.position, Quaternion.identity) as GameObject;
    }

    }

    void Update () {

    }
}

I get the following error:

Assets/Resources/Scripts/BlockTestScript.cs(14,28): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

How can I resolve this issue?

UPDATE

After looping through to instantiate the prefabs I now get the following error:

NullReferenceException: Object reference not set to an instance of an object
BlocksTestScript.Start () (at Assets/Resources/Scripts/BlocksTestScript.cs:15)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You seem to be trying to instantiate an entire array of GameObjects as a single GameObject. First things first, I would suggest looping through the array and instantiating one at a time. \$\endgroup\$ Commented Jun 14, 2017 at 16:18
  • \$\begingroup\$ @DisturbedNeo I just updated the question \$\endgroup\$ Commented Jun 14, 2017 at 16:53
  • \$\begingroup\$ blockPrefabs is null after Resources.LoadAll ("/Prefab/BlockPrefabs") as GameObject[]; so it's wrong. Also, use Instantiate(blockPrefabs[i]); instead of all this. If you have 7 prefabs, why don't you just assign them in the inspector? Above code is a bad practice cause if you move them to another folder in the future, you'll have a bad day. \$\endgroup\$
    – Jacob
    Commented Jun 14, 2017 at 17:17

1 Answer 1

2
\$\begingroup\$

In the first place look at your instantiation code: it makes no sense. You're assigning a GameObject myPrefabs (why the name myPrefab? It's not a prefab after you instantiate it; and why the s? every one your instances is ONE instance) but as soon as you go out of the for loop's scope, you've already lost that reference. You need to set up an empty array like this in your class:

GameObject blocks[] = new GameObject[7];

and then do

for (int i = 0; i < blockPrefabs.Length; i++)
    blocks[i] = Instantiate(blockPrefabs[i]) as GameObject;

...now you will maintain the references to each block in that array, and can work with these later.

Secondly I'm not sure why you'd want to complicate things by using Resources.Load on your prefabs. Just drag your individual prefabs from the Project or Hierarchy views into the inspector listing for public GameObject[] blockPrefabs on your BlocksTestScript (after setting that array's size to 7 or whatever there in the inspector). Then you can remove the first 2 lines in your Start().

public class BlocksTestScript : MonoBehaviour
{
    public GameObject[] blockPrefabs; //size gets set in inspector! drag prefabs in there!
    public GameObject[] blocks;

    void Start ()
    {   
        blocks = new GameObject[blockPrefabs.Length]; //makes sure they match length
        for (int i = 0; i < blockPrefabs.Length; i++)
        {
             blocks[i] = Instantiate(blockPrefabs[i]) as GameObject;
        }
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Precisely, what I need. Thanks a lot for the detailed explanation. \$\endgroup\$ Commented Jun 14, 2017 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .