- Home /
Spawn a gameobject in random position within an area?
hello all, the problem is I have create some prefab gameobject and place them on different position, and I use OnTriggerEnter destroy them and spawn them, however I keep destroy and spawn again again and again, the gameobject starting out of my area, so how can I spawn them in random position within an area, can somebody help me with this case, thank a lot! Sorry for my poor english, hope you guys understand.
here is my script:
public GameObject cube;
void SpawnCube(){
Vector3 position = new Vector3(Random.Range(-10.0F, 10.0F), 1, Random.Range(-10.0F, 10.0F));
Instantiate (cube, position, Quaternion.identity);
}
void OnTriggerEnter(Collider collider){
if (collider.gameObject.name == "player") {
Destroy (this.gameObject);
SpawnCube();
}
}
Something is weird in your code : you destroy the game object that handles this script and then you try to call a method from it. This can't work. You should do the spawn before destroying the game object.
I assume the cube is the same prefab that contains this script ?
$$anonymous$$iraSensei is right but ins$$anonymous$$d, what if you create Empty GameObject which will spawn your prefabs as its childs components so that way, you can store them into some List and destroy them dynamically?
Thanks for reply. Yes, the cube is same prefab and contains this scripts. But my main problem is like I made a area like (50X50), and I place the prefab in middle like vector3(0,0,0), I use the random.range make the spawn location from origin prefab -10f to 10f in axis Z, so it may change to vector3(0,0,10), and do it again, it may go vector3(0,0,20), and keep doing this, it may go vector3(0,0,50 or more), so the prefab is out of my area, so how can I random spwan them in my (50X50) area? ZyTr0n, Are you means create a empty GameObject to fixed the origin position to spawn the prefabs? thanks you guys!!
Answer by 432qwerty · Jul 29, 2014 at 04:39 PM
All you have to do is swap the line Destroy (this.gameObject); and the line SpawnCube(). You can't create another cube when you have destroyed this one and therefore destroyed this instance of the script before it creates the new cube! As for the positioning, check the scaling of your prefab.
thank you for your suggestion, can let me know more, thanks
Answer by jmgek · Jul 29, 2014 at 04:41 PM
Destroy (this.gameObject);
SpawnCube();
You are calling this script over and over again while the player is in the trigger, destroy and then spawn.
I don't fully know what you are looking for but try this. Try using.
void OnTriggerExit(Collider collider){
if (collider.gameObject.name == "player") {
SpawnCube();
}
void OnTriggerEnter(Collider collider){
if (collider.gameObject.name == "player") {
Destroy (this.gameObject);
}
thank a lot, your scripts is better, I will try this one, thanks
Your answer
Follow this Question
Related Questions
Quaternion.identity problem at random spawn 1 Answer
Spawn game object in random position on screen 1 Answer
How do i spawn a square in random position within an area but only at certain x and y intervals? 1 Answer
Randomly Place Cubes In Viewport Without Overlap 1 Answer
Generate random number and set a GameObject active. 1 Answer