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!
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.
You can instantiate a prefab using Instantiate(prefab, position, rotation).
You can destroy a game object using Destroy(gameObject).
Instantiate(prefab, position, rotation)
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:
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).
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
15 People are following this question.
Quickest method to change a variable on another gameobject.
How do you stop GameObjects from instantiating inside itself?
How can I replace an object while keeping the references to that object?
Replacing empty gameobjects with prefabs
using Contains(gameObject) to find and destroy a gameObject from a list