Search Unity

How to check if a gameobject exist in unity

Discussion in 'Scripting' started by MXPSQLServer20953OneTechGuy, Mar 13, 2021.

  1. MXPSQLServer20953OneTechGuy

    MXPSQLServer20953OneTechGuy

    Joined:
    Sep 8, 2020
    Posts:
    7
    I want to know if a gameobject exist in unity.
    btw I am using c#.
    I Destroy the player on death on this game
    I want the camera to follow the player
    this is the code snippet that I use:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class followcam : MonoBehaviour
    6. {
    7.     public GameObject livecharac;
    8.     public GameObject Deadcharac;
    9.     public Vector3 pos;
    10.     public int zset = 10;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         bool playerstatus = (GameObject.Find(livecharac.name) != null);
    21.         if (playerstatus) {
    22.              // go is an instance of an object that's present in the scene
    23.              pos = new Vector3(livecharac.transform.position.x, livecharac.transform.position.y, livecharac.transform.position.x - zset);
    24.              transform.position = pos;
    25.          } else {
    26.              // go is an instance of a prefab
    27.              pos = new Vector3(Deadcharac.transform.position.x, Deadcharac.transform.position.y, livecharac.transform.position.x - zset);
    28.              transform.position = pos;
    29.          }
    30.     }
    31. }
    32.  
    This works, but it causes this 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.
    followcam.Update () (at Assets/script/followcam.cs:28)


    is there another way to find out if a gameobject exist in unity other than this?



    P.S. I know that I fill all the fields, so don't just tell me if I haven't filled all the fields
     
  2. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    Is dead character a prefab? Your code comments hint that it is. If so, you may need to instantiate it before using its transform position.
     
  3. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    You destroyed your player. So GameObject livechar is null now. Any access on method/field/property of object which contains null will cause null reference exception. liverchar.name is the problem. In your case you don't even need to search for a player every single frame.
    You can use this, this will make more sense and should prevent an error:
    Code (CSharp):
    1. if(livechar != null)

    I highly recommend avoid using tags and especially object names. It seems like an obvious method, but you work with strings which is usually unreliable, not in your case since you read name, but in most cases.
    The most optimal way is to work with objects. You make script called Player (or PlayerMovement, or PlayerHealth, whatever only player has), you often have it already, then you bind it via field with SerializeField attribute, public field or call FindObjectOfType<Player>() in Awake/Start method and cache it into a field.
     
    Last edited: Mar 13, 2021