- Home /
Boolean value returns both true and false
Hello. =)
I've been trying to make an NGUI sprite change between 2 sprites based on a boolean function.
void Update () {
if(CanSeeTarget())
Detection.GetComponent<UISprite>().spriteName = "SeenIndicatorGreen";
else
Detection.GetComponent<UISprite>().spriteName = "SeenIndicatorRed";
}
With that code it appears to always be returning false and going to the else statement, because if I was to remove the else statement entirely, the sprite would change accordingly when the enemy sees the player, but not go back to the unseen sprite (obviously).
For debugging purposes and lack of any solution, I wrote the following:
void Update () {
print(Time.frameCount);
if(CanSeeTarget())
print(CanSeeTarget());
else
print(CanSeeTarget());
}
With that code I am getting very strange results:
At frame 146 the player was no longer seen by the enemy. Yet, before that, when he could, it would output both true and false every frame! And furthermore it looks like it's running through the code twice per frame.
Please someone find SOME solution, this is getting pathetically frustrating. Be extra blunt if it was indeed mere stupidity. Thanks =)
Answer by Eric5h5 · Jun 24, 2014 at 05:55 AM
That means you have the script attached to two objects. If the output indicates that it's running twice, then yeah, it's running twice.
Your answer
Follow this Question
Related Questions
Problem with a Bool 1 Answer
How to add a Sprite Rollover Image using a Button Array 0 Answers
Change a Variable on Key Down 5 Answers
How to Detect Which Sprite a Sprite Renderer Is Using? 1 Answer
How to repeat with Boolean? 5 Answers