- Home /
how to destroy an object then instantiate another from a prefab
I've got an object called board_new that was made from a prefab with the same name. I want to destroy the current instance and then instantiate another at a new location. Can somebody tell me how this is done? There will only be one board_new instance at any given time.
Thanks
Answer by Mike 3 · Jun 29, 2010 at 04:43 PM
Destroy(instance);
instance = Instantiate(prefab, newPos, newRot);
so do I need to attach this script to the prefab?? Right now i've got it on a controller object that simply creates and destroys other objects (and keeps score, etc).
no, anywhere you like - as long as it keeps a reference to the prefab and the instantiated object
So within my script attached to the controller, I've got
var board_new = GameObject.Find("board_new");
Destroy (board_new);
var randDist: float; //distance for the new board
randDist = Random.Range(9.0, 12.0)/3.2808398950131234 ; //this is 9 - 12 feet converted to meters
Instantiate (board_new, Vector3(randDist, .156, 0), Quaternion(0,270,0));
Need to swap it around a bit - you destroyed the object you're trying to instantiate. move the destroy after the instantiate, and it'll work fine
But how will it know which one to destroy since I will have two instances?
Your answer
Follow this Question
Related Questions
Guided Missiles help? 2 Answers
How can I destroy objects and instantiates new in Array? 2 Answers
Destroy and instantiate the game objects using keyboard event 1 Answer
how to destroy camera instatiated from prefab? 0 Answers
Could someone show me examples of how to use void OnDestroy() to instantiate an object? 2 Answers