- Home /
Beginner has a problem with sequence ;)
Hi everybody :)
Something's wrong with my code. I'm trying to make some kind of sequence by pressing GUI button: First step, is the animation of falling bomb. Then, after 2 sec. from the beginning of those animation, the scene is changing to another ("ExplosionTest"). And that's all. Two steps.
Console shows me: "WARNING: Boolean expression will always have the same value." I'm trying to make this problem for two days but I can't. Where is mistake? What I have to do to make it work? Any sugestions? Thanks a lot :)
var Bomb : GameObject;
var Anim : Animation;
var Expl : boolean;
function Start(){
Anim = Bomb.GetComponent(Animation);
}
function OnGUI(){
if(GUI.Button (Rect(1000, 570, 200, 100), "Fire")) {
Bomb.animation.Play("Fall", PlayMode.StopAll);
}
}
function CoUpdate(){
if(Bomb.animation.Play);
yield WaitForSeconds(2);
{
Application.LoadLevel("ExplosionTest");
}
}
Not sure if this is right, but it might help if you declare your boolean somewhere lol.
All I see is just the Variable at the time, I don't see it anywhere else.
Like in C# (should be very very similar to Unityscript if not the exact same!)
Do something like (in your GUI Button) After Bomb Animation put
Expl = true;
then in your CoUpdate put
if(Expl && Bomb.animation.Play){
yield WaitForSeconds(2);
{
Application.LoadLevel("ExplosionTest");
Expl = false;
}
}
What I just did was declare the Expl bool as true when button is pressed, then in your CoUpdate put (If Expl & your animation is playing) then do the rest of the code. Then changed Expl back to false.
I hope that helps!
Not sure if that will fix your problem, but I hope it does.
Answer by disc330 · Oct 24, 2014 at 01:52 AM
The cause of the warning is the line: if(Bomb.animation.Play);
The member "Bomb.animation.Play" is a method, and I think in Javascript, by simply existing, it will resolve to true. (Don't quote me on that, I'm a C# man!)
I assume that you meant to use something more like: "if(Bomb.animation.isPlaying)" in your "CoUpdate" method.
Answer by Habitablaba · Oct 23, 2014 at 09:25 PM
Play returns if the animation was able to play or not. It does not return if the animation is playing which I'm guessing is how you are meaning for it to be used in your CoUpdate method.
The Animation class has an IsPlaying method for that.
Beyond that, you have some syntax errors in your CoUpdate method around your if statement.
function CoUpdate(){
if(Bomb.animation.IsPlaying("Fall")){
yield WaitForSeconds(2);
Application.LoadLevel("ExplosionTest");
}
}
Since you don't show it anywhere, I'm also unconvinced that you are starting the CoRoutine that is supposed to run CoUpdate.
Your answer
Follow this Question
Related Questions
How do I reset a variable? 2 Answers
How can I start unity fast with my C++ engine 2 Answers
Scripting a Konami Code 1 Answer
Best Unity Practices 1 Answer