I am developing a game where the player destroys trees and rocks for resources, but i am a facing problem where everytime i destroy a rock and go to destroy another rock this message shows up.
The object of type ‘BoxCollider’ 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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shatter : MonoBehaviour
{
public GameObject destroyedVersion;
public GameObject MyGreatObject;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
StartCoroutine(InstantiateAfterDelay(7f));
}
}
IEnumerator InstantiateAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
Instantiate (destroyedVersion, transform.position, transform.rotation);
Destroy (MyGreatObject);
}
}