New to IF statements. Need help with playing animation and using IF Statements.
Hello. I have a tv remote that has a small animation on it along with a trigger. When my player looks at the remote it animates and turns on the TV. And then when the player looks at it again it will animate and turn the TV back off. However i'm semi new to scripting. to break it down: I have a quad that has a black texture for (TV OFF) and I have a quad that has a (TV ON) texture.
they are both on the TV and are inactive at the start of the game. when the trigger is hit the TV turn off at first. Then when its hit again the TV turns back on. so on and so forth. So basically i have a script that sets the quads active or inactive when the trigger is hit.... but i have 3 triggers on the remove with a bunch of crazy scripts to get this to work the way i want. And I know there is a much easier solution. (I have triggers activating other triggers, its nuts)
I was wondering if i could use if statements. I never used them and I have no idea how to go about using them with this. I have watched countless videos but I can't seem to figure out what do to.
(on trigger enter would go here) Then maybe somthing like
if (TV is ON turn TV off..........{
this.transform.GetComponent(Animation).animation.Play("myanimation");
}else{ (if TV is OFF turn TV back ON again????)
this.transform.GetComponent(Animation).animation.Stop("myanimation");
maybe another if statement???
////I would prob. need some var's in there and maybe a boolean???
}
I know that is not really a script.
Answer by GimLee · Sep 03, 2015 at 11:45 PM
Your tv has two states, on & off.
So the program will first read the: if (TV.isActive == true) // (I don't know how you are toggling it active) If that statement is NOT true, it will perfrom what you've put in "else".
Have you tried this idea of yours? Because the logic is correct in my opinion.
One thing though.
line 6: don't put another if statement there, only "else".
If the TV is NOT on, then it can probably only be off I guess. Look at this:
if (TV is ON) {
TV turn OFF
doAnimationStuff
}
else {
TV turn ON
doAnimationStuff
}
You "ask" the program something in the parenthesis, and if that statement is correct, it will perform the commands between the { }
Thanks for your quick response. to answer your 1st question I have a trigger on the remote that has a script that sets the object active and also disables the (is trigger) from the trigger. lol, So the quad becomes active via set active true. And covers up the TV ON texture. And that trigger activates a different trigger so that when I hit that trigger the quad becomes inactive via setactive false. Like is said. I have 3 triggers that help me with this buts its a huge pain and i really would like to have one script that does this all. If you saw my mess you would laugh.
And I cannot try my idea because I actually have no idea how to write it in script. I just need some help getting my idea on script. LOL
I'm a little bit confused what the actual problem is then.
In the two statements, you can enable/disable the isTrigger value if you want to. I don't really know what you're asking then. I thought you wanted help understanding if-statements :)
I'm sorry. I was hoping i could substitue my crazy mess of scripts and triggers to maybe just one script by using if statements. I don't have any. Here is what i have. $$anonymous$$aybe this will show you just how messy and crazy it is. LOL
Just a warning. This is going to be crazy but to be honest this is all i know for scripting and it works. But I would like to know more and make it a bit easier. HEHE I have to go through this every time I want to make something do something. $$anonymous$$y game is taking way longer than it should.
//// this goes on a trigger attached to the remote
var remote : GameObject;
var offanim : String;
var tvoff : GameObject;
var Tvlight : GameObject;
var seconds : float;
var nexttrigger : GameObject;
function OnTriggerEnter (player : Collider){
if(player.tag == "Player")
remote.GetComponent.<Animation>().Play(offanim);
yield WaitForSeconds(seconds);
tvoff.SetActive(true);
Tvlight.SetActive(false);
nexttrigger.SetActive(true);
}
///// This script goes on yet another trigger that the above script activated...
var trigger : GameObject;
var seconds : float;
var trigger1 : GameObject;
var triggeranim : String;
function OnTriggerEnter (player : Collider){
if(player.tag == "Player")
trigger1.GetComponent.<Animation>().Play(triggeranim);
yield WaitForSeconds(seconds);
trigger.SetActive(true);
}
////////And last but not least, the last trigger, triggers this trigger script. LOL LOL
var remote : GameObject;
var offanim : String;
var tvOn : GameObject;
var TvlightOn : GameObject;
var seconds : float;
var trigger : GameObject;
var triggeranim : String;
var destroy : GameObject;
var blacktv : GameObject;
function OnTriggerEnter (player : Collider){
if(player.tag == "Player")
trigger.GetComponent.<Animation>().Play(triggeranim);
remote.GetComponent.<Animation>().Play(offanim);
yield WaitForSeconds(seconds);
tvOn.SetActive(false);
TvlightOn.SetActive(true);
blacktv.Destroy(destroy);
}
and now i can't turn my TV back on because I would have to make a bunch more scripts and triggers. But I bet someone knows how i could take all that and make one script by using IF Statements and maybe a few other things I have no idea how to use.
Your answer
