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

Need help with simple if statement + OnMouseDown. Please help! [SOLVED]

Discussion in 'Scripting' started by radler470, Jan 11, 2015.

  1. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Earlier I posted this issue, but I want to add a level of difficulty. Names have changed based on things that make sense in my scene, but, I want the object serialIN to be set as active upon mouse click, only if serialHold is already active, otherwise do not set as active. Does that make sense? I know I can do it with an if/else statement, but I can't seem to be able to get it to work.

    td;dr I want something to happen OnMouseDown, based on the conditions of an if statement.

    Can someone please show me an example of this?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class serialIN : MonoBehaviour {
    4. public GameObject serialHold;
    5.     [SerializeField]
    6. private SerialInDoer Dd;
    7.     void OnMouseDown () {
    8.         Dd.Switch(true);
    9.     }
    10.  
    11. }
    12.  
    Very very much appreciated... thank you.
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Code (csharp):
    1.  
    2. if (serialHold.active)
    3.     Dd.Switch(true);
    4.  
    Where's the trouble?
     
    radler470 likes this.
  3. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Thank you!!!! The issue was (is) that I am an idiot when it comes to scripting. Got this particular issue working thanks to your help. One more question, if you will. I would like to assign a second condition - basically, I want serialHold to be deactivated at the same moment. How would I go about doing that?
     
  4. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    I got it working, but I am getting an error with my "else" statement - do you have any idea why? If I comment out the else portion, the if portion works properly.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class serialIN : MonoBehaviour {
    5. public GameObject serialHold;
    6.     [SerializeField]
    7.  
    8. private SerialInDoer Dd;
    9.  
    10.     void OnMouseDown () {
    11.         if (serialHold.active)
    12.             Dd.Switch (true);
    13.             serialHold.SetActive(false);
    14.  
    15.         else
    16.             Dd.Switch (false);
    17.     }
    18.    
    19. }
    20.  
     
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    An "if" statement with more than 1 line of code dependent on its condition must be enclosed with { } brackets. Same goes for all codeblocks actually (i.e. else,else if, while ,dowhile,for)
     
    radler470 likes this.
  6. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    OH. Perfect, got it. Thank you!!!!!

    Updated code for reference:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class serialIN : MonoBehaviour {
    5. public GameObject serialHold;
    6.     [SerializeField]
    7.  
    8. private SerialInDoer Dd;
    9.  
    10.     void OnMouseDown () {
    11.         if (serialHold.active)
    12.                 {
    13.                     Dd.Switch (true);
    14.                     serialHold.SetActive (false);
    15.                 }
    16.         else
    17.             Dd.Switch (false);
    18.  
    19.  
    20.     }
    21.    
    22. }
     
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483

    I would get into the habit of enclosing all blocks of code with those brackets, even the 1 liners. No chance of errors then. For example, this could be a bug difficult to spot:

    Code (CSharp):
    1. if(dontDoIt)
    2.   DoThisIfCant();
    3.   DoThisIfCantAlso();
    4.  
    5.  
    Here, the idea was to call those 2 functions if some boolean is true. The code however will call DoThisIfCant() on the condition but always call the 2nd.
     
    radler470 likes this.
  8. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Thank you!