Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Script for toggling GameObject not working.

Discussion in 'Scripting' started by Treasureman, Feb 28, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a script that's supposed to toggle a GameObject on and off when you press "C". Here's the script...
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var flame : GameObject;
    4.  
    5. function Start ()
    6. {
    7.     flame.SetActive (false);
    8. }
    9.  
    10. function Update ()
    11. {
    12.     if(Input.GetKeyDown("c"))
    13.     {
    14.         flame.SetActive (true);
    15.     }
    16.     else
    17.     {
    18.         flame.SetActive (false);
    19.     }
    20.  
    21.   }
    22.  
    But it doesn't work. No errors, just doesn't do anything. Why?
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    1.make sure the script is attached to a game object.
    2.make sure flame is set in the inspector (but assuming if you did 1, then this shouldn't matter since you had no errors)


    3. the code in Update is not a "toggle", but a "only turn it on for the exact frame you pressed c"

    try this:
    Code (CSharp):
    1.  void Update()
    2.         {
    3.             if(Input.GetKeyDown(KeyCode.C))
    4.             {
    5.                 flame.SetActive(!flame.activeSelf);
    6.             }
    7.         }
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    It still does nothing...
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var flame : GameObject;
    4.  
    5. function Start ()
    6. {
    7.     flame.SetActive (false);
    8. }
    9.  
    10. function Update ()
    11. {
    12.             if(Input.GetKeyDown(KeyCode.C))
    13.             {
    14.                 flame.SetActive(!flame.activeSelf);
    15.             }
    16.         }
    17.  
     
  4. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    is flame set to the same game object the script is on?

    if it is, your disabling your own script in Start.

    The script should be on a different game object that will not be set inactive
     
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    971
  6. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Oh, okay, it works now. I do have a different question. I have a script that make my flashlight work...
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var TorchClick : AudioClip;
    4. var flashLight : Light;
    5. var intensity : float = 7;
    6. var fadeTime : float = 0.55;
    7.  
    8. function Start ()
    9. {
    10.     flashLight.GetComponent.<Light>().enabled = false;
    11.     flashLight.GetComponent.<Light>().intensity = intensity;
    12. }
    13.  
    14. function Update ()
    15. {
    16.     if(flashLight.GetComponent.<Light>().enabled == true)
    17.     {
    18.         flashLight.GetComponent.<Light>().intensity -= 0.1 * Time.deltaTime / fadeTime;
    19.         Debug.Log(flashLight.GetComponent.<Light>().intensity);
    20.     }
    21.  
    22.     if(Input.GetKeyDown("c"))
    23.     {
    24.         GetComponent.<AudioSource>().PlayOneShot(TorchClick);
    25.        
    26.     if(GetComponent.<Light>().enabled == false)
    27.     {
    28.         flashLight.GetComponent.<Light>().enabled = true;
    29.     }
    30.     else
    31.     {
    32.         flashLight.GetComponent.<Light>().enabled = false;
    33.     }
    34.   }
    35. }
    But, for some reason, the flashlight starts enabled, even though its set as false in the start function.
     
  7. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Heres my best guess. Start() only runs once prior to the first Update() call, but the first update call will not happen unless the script is enabled and on an active game object. So i'm thinking maybe your script is disabled or set inactive?

    Perhaps try changing Start() to Awake() or checking that the script is enabled on a active game object.