Search Unity

Inventory system issues

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

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a script that I need to make 3 objects cycle through. Here's my script...
    Code (JavaScript):
    1. var radio : GameObject;
    2. var lighter : GameObject;
    3. var pipe : GameObject;
    4. function Start () {
    5.     radio.SetActive (false);
    6.     lighter.SetActive (false);
    7.     pipe.SetActive (false);
    8. }
    9. function Update () {
    10.     if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Vertical") > 0.5)
    11.          lighter.SetActive (true);
    12.          radio.SetActive (false);
    13.          pipe.SetActive (false);
    14.  
    15.     if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Vertical") < 0.5)
    16.          radio.SetActive (false);
    17.          pipe.SetActive (false);
    18.          lighter.SetActive (false);
    19.  
    20.     if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") > 0.5)
    21.          pipe.SetActive (true);
    22.          radio.SetActive (false);
    23.          lighter.SetActive (false);
    24.  
    25.     if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5)
    26.          pipe.SetActive (false);
    27.          radio.SetActive (true);
    28.          lighter.SetActive (false);
    29. }
    30.  
    31.    
    But I have a few issues

    1. the radio variable starts enabled, and won't switch off
    2. the pipe variable is the only one that works.

    I don't understand why. Could someone help?
     
  2. VikingPingvin

    VikingPingvin

    Joined:
    Jan 22, 2016
    Posts:
    16
    Try Input.GetButtonDown instead?
     
  3. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    use "else if" instead of "if" on the last 3 in statements.
    the first if statement is just fine.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You're missing some brackets, there!

    UnityScript (and C#) does not care about where your code is tabbed to, so even if you write it like this:

    Code (csharp):
    1.  
    2.    if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5)
    3.          pipe.SetActive (false);
    4.          radio.SetActive (true);
    5.          lighter.SetActive (false);
    It works like this:

    Code (csharp):
    1.  
    2.    if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5) {
    3.          pipe.SetActive (false);
    4.    }
    5.    radio.SetActive (true);
    6.    lighter.SetActive (false);
    Ie. every line not directly below one of your if-statements is always run.
    You need this:

    Code (csharp):
    1.  
    2.    if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5) {
    3.        pipe.SetActive (false);
    4.        radio.SetActive (true);
    5.        lighter.SetActive (false);
    6.    }
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. // no curly brackets
    3. if()
    4. commandControlledByIf
    5. commandNotControlledByIf
    6. commandNotControlledByIf
    7.  
    8. // curly brackets
    9. if()
    10. {
    11. commandControlledByIf
    12. commandControlledByIf
    13. commandControlledByIf
    14. }
    15.  
     
  6. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Okay, I did that, and now it starts okay with everything equaling false, but when I hold down and then let go of Fire2, the pipe variable turns on no matter what axis is held, or if they're even held at all. After that, it doesn't turn off. What's wrong now?

    Code (JavaScript):
    1. var radio : GameObject;
    2. var lighter : GameObject;
    3. var pipe : GameObject;
    4. function Start () {
    5.     radio.SetActive (false);
    6.     lighter.SetActive (false);
    7.     pipe.SetActive (false);
    8. }
    9. function Update () {
    10.     if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") > 0.5) {
    11.          lighter.SetActive (true);
    12.          radio.SetActive (false);
    13.          pipe.SetActive (false);
    14.          }
    15.    
    16.     if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Vertical") < 0.5) {
    17.          radio.SetActive (false);
    18.          pipe.SetActive (false);
    19.          lighter.SetActive (false);
    20.          }
    21.    
    22.     if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Horizontal") < 0.5) {
    23.          pipe.SetActive (true);
    24.          radio.SetActive (false);
    25.          lighter.SetActive (false);
    26.          }
    27.  
    28.     if (Input.GetButtonUp("Fire2") && Input.GetAxis ("Vertical") > 0.5) {
    29.          pipe.SetActive (false);
    30.          radio.SetActive (true);
    31.          lighter.SetActive (false);
    32.          }
    33. }
    34.    
    35.    
    Also, the code is in JavaScript. I saw you mentioned CSharp in your response, and I just wanted to make sure you knew.