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

Question Need help

Discussion in '2D' started by Stepan38, May 20, 2020.

  1. Stepan38

    Stepan38

    Joined:
    Mar 14, 2020
    Posts:
    1
    upload_2020-5-20_11-33-23.png
    How can I solve this problem?
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    This is often from keeping around a reference to a dead transform. For example, you cache some component, transform, or gameobject of an enemy that is being attacked by the player. But then it dies. When this reference is encountered again, it will report that it is dead - this error. The solution is to check to make sure that whatever thing you're about to access is not dead. This could be done by adding a bool isDead.
    Code (CSharp):
    1. if(!someThing.isDead)
    2.    DoStuff(); // attack it until it is dead
    3. else DoOtherStuff(); // find different thing to attack
    When something is killed, set isDead = true.
     
  3. GrizzlyPunchGames

    GrizzlyPunchGames

    Joined:
    May 21, 2020
    Posts:
    11
    i would reccomend doing it that way:
    If(yourObject !=null) // "yourObject" - object you are referencing,
    {
    //your script here;
    }