- Home /
OnMouseDown Boolean
Hi I'm doing some actions in a OnMouseDown function, which switches a boolean. But the problem is it executes both actions simultaneously... How can I prevent this? I already tried putting them separately in an Up and a Down function but that didn't work either. I also tried yield to make intervals before checking but that didn't work either.
function OnMouseDown (){
if(MainScript.P1Turn && CardInPlay){
Targeting = true;
}
if(MainScript.P1Turn && CardInPlay && Targeting){
Targeting = false;
}
}
It seems like you only need the second piece of code... considering Targeting can be false anyway in the first block. Is there any reason you need the first block included and not just the second block?
You are setting it false in both if statements, did you know this?
Are you trying to alternate the targeting boolean between true and false?
$$anonymous$$ust've made a copy mistake there, it's not in my actual script.
Answer by fafase · Aug 07, 2012 at 08:52 PM
You need to use else if in the second one.
if(MainScript.P1Turn && CardInPlay){
Targeting = false;
}
else if(MainScript.P1Turn && CardInPlay && Targeting){
Targeting = false;
}
This way if the first is trye the second won't do. The second will be checked only if the first one returns false.
Still, it does not make much sense since whether the first is true then the second won't happen. If the first is not true the second won't be either since you check the same thing plus target. Finally, both conditions do the same action.
Answer by Akill · Aug 07, 2012 at 09:22 PM
It seems you want to alternate between targeting = true and targeting = false.
Try this:
function OnMouseDown (){
if(MainScript.P1Turn && CardInPlay)
{
targeting = !targeting;
}
}
Your answer
Follow this Question
Related Questions
Why doesn't my script work? 1 Answer
Weird Boolean Problem c# 1 Answer
How to turn on/off multiple lights using GUI buttons? 1 Answer
Error : Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) 1 Answer
Help needed on c# code which all of you will find basic apart from me 1 Answer