My script is pretty simple. It involves a stick which I want to pick up, so when I walk into the box collider it is triggered and if I press E the stick is destroyed.
The problem is that I have a small floating ‘E’ above the stick so that the player knows which button to press in order to pick up the stick, this ‘E’ appears when you walk into the collider and is a child of the stick.
So when I destroy the stick I get an error saying that I am still trying to access the object after I have destroyed it. This is because I have publicly declared the child transform. Is there another way to setActive() the child of the stick without having to publicly declare the transform?
Here is my code…
using UnityEngine;
using System.Collections;
public class PlayerInv : MonoBehaviour {
public GameObject interactIcon;
void OnTriggerStay(Collider col){
if(col.gameObject.CompareTag("Stick"))
{
interactIcon.gameObject.SetActive (true);
if(Input.GetKeyDown(KeyCode.E))
{
Destroy(col.gameObject);
}
}
}
void OnTriggerExit(Collider col){
if(col.gameObject.CompareTag("Stick"))
{
interactIcon.gameObject.SetActive (false);
}
Debug.Log ("exiting collider");
}
}