Replace gameobject with another

Hi, in my scene i have a car, when its hit by a bullet, the car instantiates a explosion, but then i want to "disappear" the original car, and appear a destroyed car i have in my prefabs, i have no idea where to start, thanks!

more ▼

asked May 17, 2013 at 09:10 PM

avatar image

Daniel Olvera
33 4 5 8

You can instantiate the destroyed car with this:

 Instantiate(destroyedCarPrefab, originalcar.transform.position, originalcar.transform.rotation);

and destroy the original one like this:


The originalcar is a Gameobject variable filled with the original car gameobject. The destroyedCarPrefab is a Prefab or Gameobject variable filled with the prefab of the destroyed car.

May 17, 2013 at 09:18 PM ExTheSea

You can instantiate a prefab using Instantiate(prefab, position, rotation). You can destroy a game object using Destroy(gameObject).

In your case, you'd want to instantiate the prefab in the same location and rotation of the old object, and then destroy the old one and the explosion:

 GameObject destroyedCarGameObject = (GameObject)Instantiate(destroyedCarPrefab, carGameObject.transform.position, carGameObject.transform.rotation);

See the documentation here:

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

Just make sure the car prefab and the destroyed car prefab have the same center (pivot) and are facing the same direction when they're rotations are (0,0,0).

May 17, 2013 at 09:35 PM Tomer Barkan
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
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



Answers and Comments



asked: May 17, 2013 at 09:10 PM

Seen: 3562 times

Last Updated: May 17, 2013 at 09:36 PM