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

please help / missing reference error

Discussion in 'Scripting' started by bilge_adam72, Aug 10, 2022.

  1. bilge_adam72

    bilge_adam72

    Joined:
    Nov 8, 2019
    Posts:
    7
    ı have this probelm and ı dont know this is solution

    error :

    MissingReferenceException: The object of type 'GameObject' 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.
    PompaliSaldiri+<atesetme>d__17.MoveNext () (at Assets/scripts/Karaktersaldiri/PompaliSaldiri.cs:73)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <6afd1274f120405096bc1ad9e2010ba6>:0)
    UnityEngine.MonoBehaviour:StartCoroutine(String)
    PompaliSaldiri:pompaliel_Ates() (at Assets/scripts/Karaktersaldiri/PompaliSaldiri.cs:37)
    UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

    please help
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
  3. bilge_adam72

    bilge_adam72

    Joined:
    Nov 8, 2019
    Posts:
    7

    hello first of all thank you for your reply. My problem is exactly like this: I'm making an fps game and there are 4 basic weapons in this game. I built a firing system for these guns. I made a small slot system to go back and forth between weapons. The basic logic of this system is that if you click on the button of the weapon you want, it disables everything belonging to the previous weapon and activates all gameobjects that have the characteristics of the purchased weapon. I can't think of any other way for this slot system. The problem starts right here. While I am in the pistol weapon, the projectile throwing object of the pistol works very well when the pistol is active, but when the pump weapon is activated, everything related to the pistol is automatically disabled and the objects of the pumped weapon appear. When I want to shoot with the pump gun, it destroys the projectile throwing object of the pump gun after the first jam. It was a bit complicated, but unfortunately I wrote a long code, it's really hard to explain it in writing. Thank you in advance!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    You need to examine your code and find out what you are destroying and still referencing.

    SO: steps for success:

    - find out what is null (destroyed in your case)
    - find out WHY that thing got destroyed (you destroyed it? Scene change? etc.)
    - fix it, either by:

    - NOT destroying it, OR
    - NOT keeping a reference to it after it gets destroyed.

    Nobody here can do this for you, no matter how you explain it.
     
  5. bilge_adam72

    bilge_adam72

    Joined:
    Nov 8, 2019
    Posts:
    7
    I didn't write any code for it to be destroyed, that's the problem. So my logic is all about SetActive being enabled or disabled. So I didn't write anything that will destroy that object with the code, it disappears by itself.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    You can put a script on the thing that gets destroyed and do a Debug.Log() within the void OnDisable() method and see who does it, or at least know exactly when it happens.

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  7. bilge_adam72

    bilge_adam72

    Joined:
    Nov 8, 2019
    Posts:
    7
    thanks for your help. I will try this.