x


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

more ▼

asked Jun 29 '10 at 04:41 PM

Jeff Ciaccio gravatar image

Jeff Ciaccio
262 27 37 48

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
Destroy(instance);
instance = Instantiate(prefab, newPos, newRot);
more ▼

answered Jun 29 '10 at 04:43 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

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).

Jun 29 '10 at 04:48 PM Jeff Ciaccio

no, anywhere you like - as long as it keeps a reference to the prefab and the instantiated object

Jun 29 '10 at 05:02 PM Mike 3

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));
Jun 29 '10 at 05:06 PM Jeff Ciaccio

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

Jun 29 '10 at 05:11 PM Mike 3

But how will it know which one to destroy since I will have two instances?

Jun 29 '10 at 05:13 PM Jeff Ciaccio
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1667
x764

asked: Jun 29 '10 at 04:41 PM

Seen: 3446 times

Last Updated: Jun 29 '10 at 05:50 PM