How to instantiate an array of Prefabs in C# Script

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)

• 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. Jun 14 '17 at 16:18
• @DisturbedNeo I just updated the question Jun 14 '17 at 16:53
• 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. Jun 14 '17 at 17:17

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;
}
}
}

• Precisely, what I need. Thanks a lot for the detailed explanation. Jun 14 '17 at 18:03