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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Beginner question - GameObjects active or not active with Array and loop

Discussion in 'Scripting' started by Louis_Amerongen, Dec 27, 2017.

  1. Louis_Amerongen

    Louis_Amerongen

    Joined:
    Jul 21, 2017
    Posts:
    19
    Hi,

    I try to make a button where GameObjects can be activated or deactivated when clicked on a button or key. In first instance, the objects have to be visible or activated. The code beneath (in italic) this text shows my attempt to do this.
    Strange is that the Debug.Log does behaves like it should be, but the regarding the loops it only runs the loop that comes last (in this case Keycode Q = False). It looks like for example that it ignores the if (Input.GetKeyDown(KeyCode.W)) code.

    What am I doing wrong?

    Thanks in advance,

    Louis

    Code (CSharp):
    1. public class ShowHide : MonoBehaviour
    2. {
    3.  
    4.     public GameObject [] HideObjects;
    5.  
    6.  
    7.  
    8.     // Use this for initialization
    9.    void Start()
    10.     {
    11.  
    12.         for (int i = 0; i < HideObjects.Length; i++)
    13.         HideObjects[i].SetActive(true);
    14.     }
    15.    
    16.  
    17. // Update is called once per frame
    18. void Update()
    19.    {
    20.         if (Input.GetKeyDown(KeyCode.W))
    21.  
    22.                 Debug.Log("Alles aan");
    23.         {
    24.                 for (int i = 0; i < HideObjects.Length; i++)
    25.                 HideObjects[i].SetActive(true);
    26.         }
    27.         if (Input.GetKeyDown(KeyCode.Q))
    28.  
    29.                 Debug.Log("Alles uit");
    30.         {
    31.                 for (int i = 0; i < HideObjects.Length; i++)
    32.                 HideObjects[i].SetActive(false);
    33.         }
    34.     }
    35. }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Your debug.log statements have to be inside the brackets.
     
    Louis_Amerongen likes this.
  3. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    Basic background:
    'if' statements only run a single piece of code.
    The way to run more than one line of code is by enclosing multiple lines inside the {} braces. These braces group statements and declarations and effectively appears as one piece of code, hence why they are often used with conditionals.

    So:
    Code (CSharp):
    1.  
    2. if (condition)
    3.     Debug.Log("Test");
    4.  
    5. //If condition is true, Test, else nothing
    6.  
    Is exactly the same as
    Code (CSharp):
    1. if (condition)
    2. {
    3.     Debug.Log("Test");
    4. }
    5.  
    6. //If condition is true, Test, else nothing
    7.  
    However:
    Code (CSharp):
    1.  
    2. if (condition)
    3.     Debug.Log("Test");
    4.     Debug.Log("Test2");
    5.  
    is actually the same as
    Code (CSharp):
    1.  
    2. if (condition)
    3. {
    4.     Debug.Log("Test");
    5. }
    6. Debug.Log("Test2");
    7.  
    8. //If condition is true, Test, Test2
    9. //else Test2
    10.  

    Your code:
    What your code is actually doing is running the debug calls based off the if statement but only that line.
    The code within the braces is actually just part of the Update method so your code is effectively:
    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.     //If we are pressing W, debug message
    5.     if (Input.GetKeyDown(KeyCode.W))
    6.     {
    7.         Debug.Log("Alles aan");
    8.     }
    9.  
    10.     //Set all HideObjects to active
    11.     for (int i = 0; i < HideObjects.Length; i++)
    12.     {
    13.         HideObjects[i].SetActive(true);
    14.     }
    15.  
    16.     //If we are pressing Q, debug message
    17.     if (Input.GetKeyDown(KeyCode.Q))
    18.     {
    19.         Debug.Log("Alles uit");
    20.     }
    21.  
    22.     //Set all HideObjects to inactive
    23.     for (int i = 0; i < HideObjects.Length; i++)
    24.     {
    25.         HideObjects[i].SetActive(false);
    26.     }
    27. }
    28.  
    Can you see the mistake now? (edit: Comments added for clarity)

    So you are actually running a conditional debug, setting everything to active, running another conditional debug, then setting everything to inactive, every update loop! This is the cause of your debug confusion (In particular why it seemed like pressing Q was working correctly - Debug message (conditional on keypress) and all inactive (happening every frame anyway so it appeared to be working; just the activation appeared to not be working)). Hopefully this background helps you understand conceptually a bit more.

    Solution:
    As per fire7side's comment
     
    Last edited: Dec 27, 2017
  4. Louis_Amerongen

    Louis_Amerongen

    Joined:
    Jul 21, 2017
    Posts:
    19
    Wauw, Thank you very much for the extensive explanation Mick!

    Thanks fire7side you're suggestion works!

    Regards Louis