- Home /
How to instantiate a empty gameobject prefab
Hi, I am trying to build a brick breaker game and am trying to create a few empty game objects for the bricks to grab transforms from. So far I have this.
public GameObject Row1;
public GameObject Row2;
public GameObject Row3;
public GameObject Row4;
public GameObject Row5;
void start()
{
level = 0;
spawn ();
}
void spawn()
{
Instantiate(Row1,transform.position,Quaternion.identity);
Instantiate(Row2,transform.position,Quaternion.identity);
Instantiate(Row3,transform.position,Quaternion.identity);
Instantiate(Row4,transform.position,Quaternion.identity);
Instantiate(Row5,transform.position,Quaternion.identity);
}
I am not getting any errors, but it just isnt working. I have added the prefabs to the variable section of the script and the script is attacthed to an empty game controller object. Any help works, thank you.
Answer by Guhanesh · Apr 05, 2016 at 06:03 AM
There is a typo error in your script. It is void Start() not void start().Check if the spawn() function is getting called by printing something inside the function.Other than this your code should work fine.
If you want to Instantiate a empty gameobject like your question says then here is the code.
GameObject Row1;
// Use this for initialization
void Start () {
Row1=new GameObject();
spawn ();
}
void spawn()
{
Instantiate(Row1,transform.position,Quaternion.identity);
}
@Guhanesh Yeah after 15$$anonymous$$ of staring at it I found that out. Still new to c# and unity, but thank you for the help.
if my answer was helpful, you could mark it as correct or upvote it.(just for the karma :))
Sorry for the late response. Had a problem where it wouldn't let me fully sign in.
Beware that in latest Unity (2017) the behavior of this code is different!
The above code snippet will create TWO gameobjects.
The consctructor new GameObject()
instantiates gameobject by itself. Instantiate(Row1)
will create a copy of gameobject Row1.
Thanks for mentioning it. Was pretty confused when Instantiate(new GameObject, ...); caused two objects two spawn.