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 how to work around: Missing (GameObject)

Discussion in 'Scripting' started by dasruedi_unity, Jan 4, 2023.

  1. dasruedi_unity

    dasruedi_unity

    Joined:
    Jul 20, 2022
    Posts:
    10
    Hello there and a happy new year.

    I am working on a little project that has a container full of amoeba like creatures that roam around and sometimes even eat each other.
    So far everything worked out great so I started working on their little combat system. They spot a foe, set it as Target (Gameobject), move towards it and start attacking. Once the targeted creature is defeated it get's destroyed and this is where the problem comes up.
    Once they killed a foe, they freeze because their target is "Missing (GameObject)" and they don't know what to do with this information. What is the most efficient way top set it back to null so that it can proceed to find a new target?

    Thanks in advance for all Answers.


    PS: The target is set through colliders btw.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Once a GameObject reference goes missing like that, it should test as null.

    This lets you tell if it exists.

    So in other words, before using something that might have been eaten, test it.

    Code (csharp):
    1. if (myFellowAmoeba == null)
    2. {
    3.   // he's gone man, he's gone
    4.   myFellowAmoeba = null;
    5.  
    6.   // now go find a new fellow amoeba
    7. }
    Beyond that, this is just a nullref problem.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
    AngryProgrammer likes this.
  3. dasruedi_unity

    dasruedi_unity

    Joined:
    Jul 20, 2022
    Posts:
    10
    Thanks for the answer. I will check it out!

    ... but how can I check for the fellowAemoba to be null?

    upload_2023-1-4_20-34-58.png

    this is obviously not working.

    upload_2023-1-4_20-40-10.png
    but this isn't working either.

    If needed I can do a quick explanation of my other related code.
    But it is basically, the thing that it's behavior is tied to the CurrentTargent either being null or an assigned target creature, that is being assigned through Collision.
    But since it becomes "Missing(GameObject" it get's into a state that is beyond the behavior states conditions.
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    "Missing (GameObject)" is just how the inspector renders a
    GameObject
    field whose reference has gone missing.

    The first bit of code you just posted should work just fine. Note that the inspector might still saying "missing"; I've never really looked too closely at that.

    The second won't work. If
    CurrentTarget
    is a destroyed
    GameObject
    , then trying to call
    GetComponent<CreatureA>()
    will trigger an error.
     
  5. dasruedi_unity

    dasruedi_unity

    Joined:
    Jul 20, 2022
    Posts:
    10
    Well it didn't really work out but I found a workaround by adding a timer, that sets the Target to null after a few seconds.

    Thanks anyway for the answers to both of you.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
  7. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Can you show exactly the code that was causing problems for you? This is something you should figure out properly, rather than deciding you're just going to use weird hacks to get around!

    You want to be very confident in these basic principles, so that you can confidently create your game!

    Well, it's never actually null; objects just overload the == operator to produce true when compared against null. But that's just being pedantic :D

    More practically: I went and checked if the null comparison behaves any differently from the implicit bool conversion you've shown there. It does not look like there is any difference.

    So, immediately after calling Destroy(), the object is truthy and is not equal to null. One frame later, the object is falsey and is equal to null.

    (edit: i then clicked on the documentation link and discovered that this has already been described...)
     
    Kurt-Dekker likes this.