Hi, I’m trying to make simple switch. The problem is that it works (debug logs, which you can see in script below show properly) but only the SetActive(false) after turning on switch doesn’t work. GO is GameObject which is child to the GameObject with script.
public class Switch : MonoBehaviour
{
public bool playerHere;
public GameObject GO;
public float delay;
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
playerHere = true;
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
playerHere = false;
}
}
// Update is called once per frame
void Update()
{
delay = delay + Time.deltaTime;
if (Input.GetButton("Interact") && playerHere && delay > 1f)
{
if (GO.activeSelf)
{
Debug.Log("Turning off");
GO.SetActive(false);
delay = 0f;
}
if (!GO.activeSelf)
{
Debug.Log("Turning on");
GO.SetActive(true);
delay = 0f;
}
}
}
}