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)
blockPrefabs
is null afterResources.LoadAll ("/Prefab/BlockPrefabs") as GameObject[];
so it's wrong. Also, useInstantiate(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\$