If conditions doesn't work?!?
I have no idea why this isn't working. Can someone help me? This is my function. The script is attached to a 2D sprite.
function OnCollisionEnter2D ( collision : Collision2D )
{
var force : float = collision.relativeVelocity.magnitude / 20;
if( force > 0.2f );
{
Debug.Log(force);
GetComponent(AudioSource).volume = force;
GetComponent(AudioSource).Play();
}
}
These are some of the debug values:
0.006967617
0.09649915
0.05660962
1.204138
etc... etc...
The condition is not respected and I have no clue why.
This is most likely happening because of the semicolon here if( force > 0.2f );
. You probably just need to remove it.
$$anonymous$$y goodness. Ahahah. Thanks. I have looked for a long time, anywhere but there. Why doesn't Unity give me an error? What does the compiler understand when there are braces without any statement or declaration? Just nothing or is it somehow useful?
In this case, there isn't really an error to throw. Your code is being seen like this:
if( force > 0.2f )
{
; //empty statement does nothing
}
{
Debug.Log(force);
GetComponent(AudioSource).volume = force;
GetComponent(AudioSource).Play();
}//code block that is in its own scope. It is not part of the if statement and will be run every time
Your answer

Follow this Question
Related Questions
UnityEngine.UI.Image keeps removing itself from a Prefab. 1 Answer
Weird white background in scene view 1 Answer
How do I fix my UnityEngine Reference? 1 Answer
The If statement condition is false but the if statement stills executes 1 Answer
Frame Debugger doesn't let me input script to objects or insert objects. 0 Answers