Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question GameObject Disappears after script runs

Discussion in 'Scripting' started by Mohamad_XD, Aug 23, 2022.

  1. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    Please Help ;(
    I have a my gun with a child object called 'Gun tip'. I added a particle system to the Gun tip and the script to run the particles in the gun. When I play my project i can shoot once -the particle system works fine- but the 'Gun tip' Object gets destroyed. I got an error saying that the GameObject Particle system was destroyed but the script is trying to access it :
    upload_2022-8-23_13-17-38.png

    Please answer me


    also I'm running Unity 2019.4.40f1
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Do you destroy the particle system at any point?
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  4. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    No i didn't
    and to ensure it wasn't in the code i copy pasted :)
    i checked it and nothing contained destroy();
     
  5. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    thx for ur answer but I've tried this before. But i have used the following code to disable / enable the particle system when left clicked :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Particlesscript : MonoBehaviour
    6. {
    7.     public GameObject gun;
    8.     private ParticleSystem Particles;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         Particles = gun.GetComponent<ParticleSystem>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (Input.GetMouseButtonDown(0)) {
    19.             Particles.Play();
    20.         }
    21.         else
    22.         {
    23.             Particles.Stop();
    24.         }
    25.     }
    26. }
    27.  
     
  6. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
  7. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Check your scene view to make sure that the particle system is still there when the error is present.

    The Stop function should be perfectly fine for your application, though, do look into the different overloads of the function too, just in case.

    The error that you're getting can only be obtained if the object that you are trying to reference, I.E. ParticleSystem in your case, gets destroyed.

    In this case it does not just mean the GameObject that the particle system is attached to but also the ParticleSystem component on the GameObject in question.

    This error can also present if any parents of the ParticleSystem's GameObject are destroyed, so keep that in mind too.
     
  8. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    The error presents when the particle system gets deleted with the Gun Tip
     
  9. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Sounds about right. You should destroy the Particlescript alongside the Gun Tip, so that it doesn't try to access the particle system when it doesn't exist. Alternatively (but not preferred), just check that the particle system exists before trying to use it.
     
  10. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    The get the error just when the Gun tip get destroyed
    (the gun tip has the particle system)
     
  11. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Lets understand the error a little more:
    Code (CSharp):
    1. MissingReferenceException: The object of type 'ParticleSystem' has been destroyed but you are still trying to access it.
    The first important part is the MissingReferenceException, it tells us that we are referring to something that is missing.
    The second important part is the type, it tells us that an object of the type ParticleSystem is the culprit
    The third part is that said object has been destroyed
    And the final, most important part, is that we're still trying to access it even though it doesn't exist anymore.

    How can we prevent this?
    Well we just need to stop trying to access the particle system if it doesn't exist anymore.

    Imagine you're talking to someone and they just walk away with no warning, you'd stop talking right? Well right now you're continuing the conversation even though they aren't there anymore.
     
  12. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    Thank you, i understand what u mean but i dont even want the particle system to get destroyed. I need particles for my gun cuz my game is boring.
     
  13. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    In that case, you need to move the ParticleSystem off of your gun tip.
     
  14. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    i'll try that rn thx
     
  15. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    ... so did you check what the Stop Action is set to?
     
  16. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    Yea upload_2022-8-23_16-35-38.png
     
  17. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    Um yea i found that the particle system is DESTROYING ITS SELF :(
     
  18. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    Thanks u i fixed it :D
     
  19. Mohamad_XD

    Mohamad_XD

    Joined:
    Nov 6, 2021
    Posts:
    12
    Thank u i fixed it ;D