Unity 2D Instantiating a prefab with it's components

Posted by TazmanNZL on Stack Overflow See other posts from Stack Overflow or by TazmanNZL
Published on 2014-06-08T20:21:09Z Indexed on 2014/06/08 21:24 UTC
Read the original article Hit count: 142

Filed under:
|

I have some block prefabs that I add to an array of game objects:

public GameObject[]blocks;

Each prefab has a BoxCollider2D, Script & Rigidbody2D components. But when I try to instantiate the prefab in the scene it doesn't appear to have the components attached?

Here is how I am instantiating the prefab:

for (int i = 0; i < 4; i++) 
{
     for (int j = 0; j < gridWidth; j++) 
     {
      blockClone = Instantiate (blocks [Random.Range (0, blocks.Length)] as GameObject, new Vector3 (j, -i-2, 0f), transform.rotation) as GameObject;
     }

}

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about unity3d