- Home /
Spawning objects from moving objects, In C#
Noob question. But i'm working on this project for class, a "kaboom" like game. Where as the top moving object, the computer(in this case it's also a UFO) spawns spheres(or bombs in this case) from it as it moves. The spheres fall to the bottom of the screen. You have to catch them with a platform. But all I want to know is how to spawn them from the UFO object. I looked up instantiate and prefabs, but am a little confused. Will appreciate any help. Thanks.
Answer by Berenger · Mar 26, 2012 at 08:54 PM
So, prefabs are assets in your project folder containing a gameObject and it's hierarchy you want to re-use. It's actually a GameObject saved on the computer.
Now, the Instantiate function can copy a GameObject, for example that prefab. So, in your UFO, add in your script and drag drop the Bomb prefab on the variable (in the inspector):
JS :
var prefab : GameObject;
function CreateBomb(){
// That will copy the prefab into the scene at the UFO position
// and without rotation, just as if you dragged it in
// You don't have to affect it to a var, unless you need to.
var bomb: GameObject = Instantiate( prefab,
transform.position, Quaternion.identity;
}
C# (must be inside a class inheriting from MonoBehaviour:
GameObject prefab;
public void CreateBomb(){
GameObject bomb = Instantiate( prefab,
transform.position, Quaternion.identity;
}
Thanks! I'm going to try that out. Is this C# or java? I'm using C# for this game.
Good job, C# is the stuff ! I use js by default here though, as most beginners use it. I added the C# version, which isn't that different.
Your answer
Follow this Question
Related Questions
Massive amounts of objects. 0 Answers
Moving multiple objects independenty on mobile using touchscreen 2 Answers
Trouble "Spawning" Objects on client (UNET networking) 0 Answers
Destroy is not working with moving objects 1 Answer
Random object spawn? 1 Answer