why is GameObject.SetActive Destroying my GameObject?

why is GameObject.SetActive Destroying my GameObject from hierarchy?

heres my script:

using UnityEngine;

public class enter : MonoBehaviour
{
    private GameObject player;
    private GameObject vehicle;
    public Collider Trigger;

    void OnTriggerEnter(Collider Trigger)
    {
        player = GameObject.FindWithTag("Player");
        vehicle = GameObject.FindWithTag("Vehicle");

        if (Input.GetKeyDown(KeyCode.I))
        {
            player.SetActive(false);
            vehicle.SetActive(true);
        }
    }

    void OnTriggerStay(Collider Trigger)
    {
        if (Input.GetKeyDown(KeyCode.O))
        {
            player.SetActive(true);
            vehicle.SetActive(false);
        }
    }
}

Short answer: it’s not. Something else is.

Note also that your choice of ‘Trigger’ to name your inbound arguments effectively silently hides your member variable of the same name.

1 Like