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?
using UnityEngine;
using System.Collections;
public class serialIN : MonoBehaviour {
public GameObject serialHold;
[SerializeField]
private SerialInDoer Dd;
void OnMouseDown () {
Dd.Switch(true);
}
}
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?
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.
using UnityEngine;
using System.Collections;
public class serialIN : MonoBehaviour {
public GameObject serialHold;
[SerializeField]
private SerialInDoer Dd;
void OnMouseDown () {
if (serialHold.active)
Dd.Switch (true);
serialHold.SetActive(false);
else
Dd.Switch (false);
}
}
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)
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:
if(dontDoIt)
DoThisIfCant();
DoThisIfCantAlso();
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.