I am making a 3d game and i made it so whenever the player collides with a object tagged “walls”. he gets turned off and the camera, you lose,text, and retry button get activated. but something kinda broke and whenever i die and click the retry button the error comes and it wont let me retry. what is supposed to happen is that i get teleported back to spawn and the other things get turned off again. im new to coding so idk what this is.
here is the 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.
button.Update () (at Assets/Scripts/button.cs:23)
here is the button script:-----------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class button : MonoBehaviour
{
public GameObject player;
public GameObject deadcam;
public GameObject deadtext;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
player.transform.position = new Vector3(78.3f, 1.55f, 0f);
player.SetActive(true);
deadcam.SetActive(false);
deadtext.SetActive(false);
gameObject.SetActive(false);
}
}
}
and here is the player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class movment : MonoBehaviour
{
float xInput;
float zInput;
public float speed;
Rigidbody rb;
public GameObject winText;
public GameObject deadCam;
public GameObject deadText;
public GameObject button;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
xInput = Input.GetAxis("Horizontal");
zInput = Input.GetAxis("Vertical");
rb.AddForce(xInput * speed,0,zInput * speed);
if(Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * 200);
}
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Walls")
{
Destroy(gameObject);
gameObject.SetActive(false);
deadCam.SetActive(true);
deadText.SetActive(true);
button.SetActive(true);
}
if(collision.gameObject.tag == "Win")
{
Destroy(collision.gameObject);
winText.SetActive(true);
//SceneManager.LoadScene("level2");
}
}
}