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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class followcam : MonoBehaviour
{
public GameObject livecharac;
public GameObject Deadcharac;
public Vector3 pos;
public int zset = 10;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
bool playerstatus = (GameObject.Find(livecharac.name) != null);
if (playerstatus) {
// go is an instance of an object that's present in the scene
pos = new Vector3(livecharac.transform.position.x, livecharac.transform.position.y, livecharac.transform.position.x - zset);
transform.position = pos;
} else {
// go is an instance of a prefab
pos = new Vector3(Deadcharac.transform.position.x, Deadcharac.transform.position.y, livecharac.transform.position.x - zset);
transform.position = pos;
}
}
}
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