- Home /
How do i have explosion effect after my object destroyed?
I am having Game object i have made prefab of this game object and already added particle system to my prefab. I have placed this game object in the scene and i started my particle system without instantiate it in my code. Now i want to make explosion effect after i destroy my game object,.. So what should i do for this?? Please Guide me. Thanks for your help and support in Advance.
here is my code [Edited] :
function Shoot(){
if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
var hit: RaycastHit;
//var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray ,hit ,200))
{
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
var pickupSpawnPoints:GameObject = hit.collider.gameObject;
var clone : GameObject;
if(pickupSpawnPoints.tag == "Pickup")
{
clone = Instantiate(explosion,GameObject.FindGameObjectWithTag("Player").transform.position, transform.rotation);
Destroy(hit.collider.gameObject , 5);
}
}
}
Ok, it looks like I answered your Duplicate Question : http://answers.unity3d.com/questions/407498/missingreferenceexception-the-object-of-type-trans-1.html
clone = Instantiate(explosion,GameObject.FindGameObjectWithTag("Player").transform.position, transform.rotation);
This is a bad way to use Find. What if the object is not found? Then errors galore. If you are trying to find the player, do it once in the start function, and store a reference to that object when you have.
var playerObject : Gameobject;
function Start()
{
if ( !playerObject ) // no object in the Inspector
{
playerObject = GameObject.FindGameObjectWithTag("Player");
if ( !playerObject ) // still no object in the Inspector
{
Debug.LogWarning( "The PLAYER was not found ...." );
}
}
}
// in function
clone = Instantiate( explosion, playerObject.transform.position , transform.rotation );
Answer by Chronos-L · Feb 27, 2013 at 07:41 AM
A simple C# script:
public Explode : MonoBehaviour {
public ParticleSystem explosion;
public void Explode( Vector3 position ) {
Instantiate( explosion, position, Quartenion.identity );
}
}
You should call the Explode( transform.position )
right before your object is Destroy()
. You can either have this as a separate script or you can incorporate it into your existing script, it largely depends on your code structure.
$$anonymous$$issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation)
GunController.Shoot () (at Assets/Scripts/GunController.js:26)
GunController.Update () (at Assets/Scripts/GunController.js:9)
it throws me such error.
This error stats that you are destroying original object ins$$anonymous$$d of the clone of it....So first instantiate the object to get the clone and then instantiate the Explosion prefab's clone to have the Explosion effect....Enjoy
hey i have edited my code plese check and solve my error..
Does it still produce the same set of errors, after you edit it?
I want to point out that in your current code, you will instantiate the explosion at the object you attached the GunController
script to, not the collided object.
yes i have attached my script to firstpersoncontroller and i have edited my code in my question. but still same error.
Answer by robertbu · Feb 27, 2013 at 07:48 AM
One easy way is to differ the destroy of your game object. Instead when the game object is hit hide it:
renderer.enabled = false;
Depending on your game, you may also have to:
collider.enabled = false;
rigidbody.active = false;
The you can do something like:
Invoke("DelayedDestroy", 1.5f);
Which calls this method 1.5 seconds later:
void DelayedDestory() {
Destroy(gameObject);
}
You can also attach the particle system to another game object and either Instantiate() or move that game object to the position of the explosion just before you destroy the game object. If you have only one explosion at a time, this solution would work well.
Answer by Maulik2208 · Feb 27, 2013 at 09:53 AM
If you are a absolute beginner then you should go to this link ----> 3dBuzz.com
And as the concern with your answer @robertbu and @Chronos-L both have pointed Good ways to Solve your problem
Don't Forget to mark the answer if found useful...Enjoy...Cheers...