Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
This question was closed Apr 03, 2016 at 09:22 AM by Kurtisi.
avatar image
0
Question by Kurtisi · Mar 31, 2016 at 09:47 PM · instantiateprefabparticlesystemparticleinstantiate prefab

How to instantiate particle effect after destroying it

Hello, i have been doing a game where i want to instantiate a particle effect when i press 'K' and after 3 seconds destroy it. But i want to do it each time i press 'K'. With this code i get an error saying: Missing Reference Exception, the object has been destroyed but you are still trying to access it. so if anyone could give me some sort of advice how to fix this problem. This is the code i have been using.

 public GameObject probaParticle;
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         if (probaParticle != null) 
         {
             if (Input.GetKeyDown (KeyCode.K)) 
             {
                 probaParticle = Instantiate (probaParticle, transform.position, Quaternion.identity) as GameObject;
                 Destroy (probaParticle, 3);
             }
         }
     }
Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
4
Best Answer

Answer by Cherno · Mar 31, 2016 at 11:04 PM

When you Instantiate, you don't assign the instantiated clone of probaParticle to a new GameObject variable, but rather assign it to the prefab you just used to instantiate a clone of. That means that if you destroy probaParticle the variable becomes null and you can't instantiate a clone of it again.

The solution of course is to just declare a new (temporary) variable for the clones.

 if (Input.GetKeyDown (KeyCode.K)) 
              {
                  GameObject probaParticleClone = Instantiate (probaParticle, transform.position, Quaternion.identity) as GameObject;
                  Destroy (probaParticleClone , 3);
              }
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 Kurtisi · Apr 01, 2016 at 05:33 PM 0
Share

Totally ignored that fact.. Thanks, i appreciate it.

avatar image
1

Answer by AGC · Apr 01, 2016 at 04:04 AM

change your code to this

 public GameObject probaParticle;
  
      // Use this for initialization
      void Start () {
      }
      
      // Update is called once per frame
      void Update () {
          if (probaParticle != null) 
          {
              if (Input.GetKeyDown (KeyCode.K)) 
              {
                  GameObject gObject = Instantiate (probaParticle, transform.position, Quaternion.identity) as GameObject;
                  Destroy (gObject, 3);
              }
          }
      }

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

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

My objects instantiate at a strange z coordinate 0 Answers

Prefab using same particle System ? 0 Answers

Spawn blood particle system prefab where sword hits enemy 1 Answer

Instantiate is spawning too many clones 2 Answers

How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer


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