Search Unity

Enable Disable Gameobjects

Discussion in 'Scripting' started by RoyS, Dec 25, 2018.

  1. RoyS

    RoyS

    Joined:
    Jan 12, 2009
    Posts:
    664
    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):
    1. using UnityEngine;
    2.  
    3. public class EnableDisable : MonoBehaviour
    4. {
    5.     public GameObject Fire001;  //Fire001 is the light/particles of the fire in Unity
    6.     public bool objactiveon = false;
    7.     bool enter = false;
    8.  
    9.     void Start()
    10.     {
    11.     }
    12.  
    13.     //Main function
    14.     void Update()
    15.     {
    16.         if (Input.GetKeyDown(KeyCode.F) && enter)
    17.         {
    18.             Fire001.gameObject.SetActive(true);
    19.         }
    20.         else
    21.         {
    22.             Fire001.gameObject.SetActive(false);
    23.         }
    24.     }
    25.     void OnGUI()
    26.     {
    27.         if (enter)
    28.         {
    29.             GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "F to light/extinguish fire");
    30.         }
    31.     }
    32.  
    33.     //Activate the Main function when player is near the gameobject
    34.     void OnTriggerEnter(Collider other)
    35.     {
    36.         if (other.CompareTag("Player"))
    37.         {
    38.             enter = true;
    39.         }
    40.     }
    41.  
    42.     //Deactivate the Main function when player is away from gameobject
    43.     void OnTriggerExit(Collider other)
    44.     {
    45.         if (other.CompareTag("Player"))
    46.         {
    47.             enter = false;
    48.         }
    49.     }
    50. }
    51.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    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).
     
  3. RoyS

    RoyS

    Joined:
    Jan 12, 2009
    Posts:
    664
    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.
     
  4. Code (CSharp):
    1. Fire001.gameObject.SetActive(true);
    ->
    Code (CSharp):
    1. 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.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    you should be able to say that afaik.

    you can go
    Code (CSharp):
    1. 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):
    1. if (Input.GetKeyDown(KeyCode.F) && enter)
    2.         {
    3.             Fire001.gameObject.SetActive(!Fire001.gameObject.activeSelf);
    4.         }
    5.  
    the exclamation mark flips the bool, also as @Lurking-Ninja said, you don't need to say gameObject again(but you can).