- Home /
"Your script should either check if it is null or you should not destroy the object."...what am i doing wrong sorry im a noobie
using UnityEngine; using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
}
public void TakeDamage(float Damage)
{
if (gameObject != null)
{
// Do something
Destroy(gameObject);
}
}
}
"gameObject" refers to the gameobject the script is attached to, so that if statement is not needed.
A little more info would be helpful. I can assume based on the code that you're calling TakeDamage from another script. It might help to have that script as well so we can help you figure out the issue. Is the error pointing to a specific line of the script?
I totally agree with iyonb96 , and the problem doesn't seem to be from this script because the gameobject should just get destroyed with no trouble as the statement will always be true.
Answer by annoying_brother · May 23, 2020 at 09:04 AM
Depends what is gameObject?
gameObject Isn't defined gameObject is used for something like transform.parent.gameObject it defines thats it is not transform but it is the whole thing!