- Home /
Spawn object problem
Hello, I'm starting today to use unity. I want to create objects when I'll press "x", so I use this code:
void Update ()
{
if( Input.GetKeyDown(KeyCode.X))
{
spawnLocation = new Vector3(Random.value * 10,Random.value * 10,0);
Enemy = GameObject.Find("Nemico");
Instantiate(Enemy, spawnLocation, Quaternion.identity);
}
}
But when I use this code I have to insert before Enemy object in my scene that it will be visible immediatly. So what is the correct procedure to create object in order to they aren't visible early in the scene?
Answer by oatsbarley · Jan 25, 2014 at 05:52 PM
If I'm understanding you, your problem is that you want to instantiate an Enemy
object, but you don't want to have to keep an object in your hierarchy to use in the Instantiate
function. If this is the case, you need to look into prefabs; specifically, you should create a prefab of the Enemy object, then instantiate from that prefab instead of from a scene object. Doing that will allow you to instantiate without having an unnecessary object in the scene.