Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ekta-Mehta-D · Feb 27, 2013 at 06:52 AM · particlesparticlesystemexplosion

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);
         }
     }              
 }
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Feb 28, 2013 at 05:01 AM 0
Share

Ok, it looks like I answered your Duplicate Question : http://answers.unity3d.com/questions/407498/missingreferenceexception-the-object-of-type-trans-1.html

avatar image Ekta-Mehta-D · Feb 28, 2013 at 05:17 AM 0
Share

yes sir.. but still i am having same error.

avatar image Chronos-L · Feb 28, 2013 at 05:24 AM 0
Share

Which line is producing the error?

avatar image Ekta-Mehta-D · Feb 28, 2013 at 05:30 AM 0
Share

clone = Instantiate(explosion,GameObject.FindGameObjectWithTag("Player").transform.position, transform.rotation);

avatar image AlucardJay · Feb 28, 2013 at 05:35 AM 0
Share

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 );
Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ekta-Mehta-D · Feb 27, 2013 at 09:55 AM 0
Share
 $$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.

avatar image Maulik2208 · Feb 27, 2013 at 10:05 AM 0
Share

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

avatar image Ekta-Mehta-D · Feb 27, 2013 at 10:13 AM 0
Share

hey i have edited my code plese check and solve my error..

avatar image Chronos-L · Feb 28, 2013 at 04:03 AM 0
Share

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.

avatar image Ekta-Mehta-D · Feb 28, 2013 at 05:16 AM 0
Share

yes i have attached my script to firstpersoncontroller and i have edited my code in my question. but still same error.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ekta-Mehta-D · Feb 27, 2013 at 10:14 AM 0
Share

i have edited my question. Please check my code.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Explosion Using Particle System 0 Answers

Particles behind/in front of other particles 1 Answer

IsAlive() and Simulate() of ParticleSystem used simultaneously 0 Answers

2d explosion Particle effect hard to make 1 Answer

Spawned Particle System in game looks different from prefab 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges