I've googled different topics on this and can't seem to get this script working. I would like to have the fire in the fireplace disabled initially. When the player walks in, he can light the fire (enable) or extinguish it (disable)...anytime he wants (not just a one time event). The "Trigger" is the cube with the rendering turned off, but collision is on. Code (CSharp): using UnityEngine; public class EnableDisable : MonoBehaviour { public GameObject Fire001; //Fire001 is the light/particles of the fire in Unity public bool objactiveon = false; bool enter = false; void Start() { } //Main function void Update() { if (Input.GetKeyDown(KeyCode.F) && enter) { Fire001.gameObject.SetActive(true); } else { Fire001.gameObject.SetActive(false); } } void OnGUI() { if (enter) { GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "F to light/extinguish fire"); } } //Activate the Main function when player is near the gameobject void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) { enter = true; } } //Deactivate the Main function when player is away from gameobject void OnTriggerExit(Collider other) { if (other.CompareTag("Player")) { enter = false; } } }
If you want the player to automatically trigger the trigger, all the collision/trigger rules apply: player must have a rigidbody, gameobject the collider is on must be enabled, etc. If you want the player to look at the fireplace and get a button appear, that's a bit more involved, but usually you raycast a few feet in front of the player, see if it hits a collider, and look if that object also has a script that can be called in some way to turn on/off the fire, and in that case enable a UI object for the button, and add a listener to the button. In all cases, you have to be sure the collider never gets turned off, otherwise once off you can't get it back on. Generally the on/off portions would be child gameobject(s).
The collider works and the message "F to light/extinguish fire", but the fire doesn't extinguish or light. The fire is initially enabled, but won't disable or enable. It's gotta be something simple stupid, but I'm not seeing it.
Code (CSharp): Fire001.gameObject.SetActive(true); -> Code (CSharp): Fire001.SetActive(true); And the other one too. And do not forget to drag and drop the game object in the inspector into the Fire001 slot.
you should be able to say that afaik. you can go Code (CSharp): transform.gameObject.transform.gameObject.transform.transform.gameObject and on and on and on forever I think the problem is the enter bool, as @Kurt-Dekker said, you need a rigidbody in the mix to get the trigger call, you need to have the "IsTrigger" enabled... SCRATCH THAT, I think the problem is the 'else', one frame after you activate the fire you turn it off. change it to this: Code (CSharp): if (Input.GetKeyDown(KeyCode.F) && enter) { Fire001.gameObject.SetActive(!Fire001.gameObject.activeSelf); } the exclamation mark flips the bool, also as @Lurking-Ninja said, you don't need to say gameObject again(but you can).