Search Unity

Activating and deactivating game objects with if statements???

Discussion in 'Scripting' started by gamegirl1984, Sep 4, 2015.

  1. gamegirl1984

    gamegirl1984

    Joined:
    Nov 6, 2014
    Posts:
    102
    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 crazy)

    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

    Code (JavaScript):
    1. (on trigger enter would go here) Then maybe somthing like
    2.    
    3.          if  (TV is ON turn TV off..........{
    4.              this.transform.GetComponent(Animation).animation.Play("myanimation");
    5.            
    6.              }else{    (if TV is OFF turn TV back ON again????)
    7.                  this.transform.GetComponent(Animation).animation.Stop("myanimation");
    8. maybe another if statement???
    9.                        ////I would prob. need some var's in there and maybe a boolean???
    10. }
    I know that is not really a script. please help!!!!
     
    Last edited: Sep 4, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    can you reformat that pseudo code chunk so it's readable? it's making my head hurt trying to make sense of it! :p

    by the sounds of it you're looking at if statements of a switch statement based on the "current state"
     
  3. gamegirl1984

    gamegirl1984

    Joined:
    Nov 6, 2014
    Posts:
    102
    sorry i couldnt figure out how to do that but i did finally. And i might as well post the codes i'm using to get my TV to turn on and of so you can see what i know and don't know. and how crazy it is.

    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. My game is taking way longer than it should.

    Code (JavaScript):
    1.  //// this goes on a trigger attached to the remote
    2. var remote : GameObject;
    3. var offanim : String;
    4. var tvoff : GameObject;
    5. var Tvlight : GameObject;
    6. var seconds : float;
    7. var nexttrigger : GameObject;
    8. function OnTriggerEnter (player : Collider){
    9. if(player.tag == "Player")
    10. remote.GetComponent.<Animation>().Play(offanim);
    11. yield WaitForSeconds(seconds);
    12. tvoff.SetActive(true);
    13. Tvlight.SetActive(false);
    14. nexttrigger.SetActive(true);
    15. }
    16. ///// This script goes on yet another trigger that the above script activated...
    17. var trigger : GameObject;
    18. var seconds : float;
    19. var trigger1 : GameObject;
    20. var triggeranim : String;
    21. function OnTriggerEnter (player : Collider){
    22.       if(player.tag == "Player")
    23.    trigger1.GetComponent.<Animation>().Play(triggeranim);  
    24.     yield WaitForSeconds(seconds);
    25.     trigger.SetActive(true);
    26.    
    27.       }
    28. ////////And last but not least, the last trigger, triggers this trigger script. LOL LOL
    29. var remote : GameObject;
    30. var offanim : String;
    31. var tvOn : GameObject;
    32. var TvlightOn : GameObject;
    33. var seconds : float;
    34. var trigger : GameObject;
    35. var triggeranim : String; //// this is to disable the trigger so it won't re-trigger...
    36. var destroy : GameObject;
    37. var blacktv : GameObject;
    38. function OnTriggerEnter (player : Collider){
    39. if(player.tag == "Player")
    40. trigger.GetComponent.<Animation>().Play(triggeranim);
    41. remote.GetComponent.<Animation>().Play(offanim);
    42. yield WaitForSeconds(seconds);
    43. tvOn.SetActive(false);
    44. TvlightOn.SetActive(true);
    45. blacktv.Destroy(destroy);
    46. }
     
  4. EETechnology

    EETechnology

    Joined:
    Aug 15, 2015
    Posts:
    185
    Looking at the scripts and at your posts I dont really get the point and I dont really understand what you want to achieve. Could you explain it again please? More in details this time!!!!!
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    +1 for more clarity needed... it's good that you found the code tags :) but your code is still hard to read. Hopefully it's a problem with the paste into the forums, but try to indent and use white space to break things up so it is more easily read

    i.e.

    Code (csharp):
    1.  
    2. var myVariable;
    3.  
    4.  
    5. function myFunctionName()
    6. {
    7.     // stuff here
    8.     if(//some clause//)
    9.     {
    10.         // some event
    11.     }
    12.     // other stuff
    13. }
    14.  
    15. function myOtherFunctionName()
    16. {
    17.     // stuff here
    18.     if(//some clause//)
    19.     {
    20.         // some event
    21.     }
    22.     // other stuff
    23. }
    24.  
    also if that code is three separate scripts, please use three code blocks and, given that it's unityscript, the file names. Helps put the pieces in their place :D
     
  6. gamegirl1984

    gamegirl1984

    Joined:
    Nov 6, 2014
    Posts:
    102
    I'm want to trigger my TV to turn OFF then ON by looking at at the remote that has a trigger on it.

    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.

    I don't know how else to explain it. I just want to turn my TV off and ON without having to use my huge mess of scripts. I don't think its all necessary.
     
  7. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    I don't know UnityScript/JavaScript very well, but you basically need a True/False boolean variable that keeps up with whether the TV is on or off if I get what you are saying.

    You shouldn't need a ton of scripts for it.

    I don't know how declaring booleans in JS works... but I will assume it works like the following:

    edit:
    Code (JavaScript):
    1. var IsTVTurnedOn : false
    I believe that's the correct syntax ^, you could also change false to true on declaration, depends which you want to be the default.

    Then basically everything will run off of that True/False flag.

    Something along the lines of(no actual code):

    If player enters trigger:
    1. switch the above boolean to its opposite state.... in C# I would do this as IsTVTurnedOn = !IsTVTurnedOn
    (You could simply do this as an if/else statement: if isTVTurnedOn == True, isTVTurnedOn = False, else
    the opposite)
    2. play animation based on that. which, if you are using different animations, would be another if loop inside this current if statement.
    ex. if IsTVTurnedOn, play onAnimation and set your OnTV object to active, set OffTV to inactive
    else play offAnimation., set OnTV object to inactive, OffTV to active.


    Booleans are a massive boon to this kind of stuff. The "!"IsTVTurnedOn in my example simply means "not", which just flips from True to False and vice versa. Not False = True and Not True = False. I don't know if you can use the "!" toggle in JS or not though, that might only work on the C# version.

    Also, generally if you simply say "if IsTVTurnedOn", this would be the same as saying if this boolean is set to True.

    Like I said, I don't fully understand all that JS style stuff and I don't 100% know what you are doing with the animations and such, but I hope that kind of helps. Someone who understands JS better than I can clarify boolean switching with it, hopefully.
     
    Last edited: Sep 4, 2015
  8. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Ok, I dont use UnityScript so this may not compile. But on your remote you probably need to do something like:

    Code (JavaScript):
    1. var IsTVOn : false;
    2.  
    3. function Start()
    4. {
    5.    /// start turned off
    6.    TurnOffTheTV();
    7. }
    8.  
    9. function OnTriggerEnter(_other: Collider)
    10. {
    11.       if(_other.gameObject.name == "Player")
    12.      {
    13.              IsTVOn = !IsTVOn;
    14.               if(IsTVOn)
    15.               {
    16.                        TurnOnTheTV();
    17.               }
    18.              else
    19.             {
    20.                       TurnOffTheTV();
    21.              }
    22.      }
    23. }
    24. function TurnOnTheTV()
    25. {
    26. // stuff you do when the tv has been turned on
    27. }
    28.  
    29. function TurnOffTheTV()
    30. {
    31. //stuff you do when the tv has been turn off
    32. }
     
  9. gamegirl1984

    gamegirl1984

    Joined:
    Nov 6, 2014
    Posts:
    102
    thanks for this. I am going to play around with it. I'm just so confused right now. I have spent hours trying to figure this out. Once I figure this out I will be able to do so much more.
     
  10. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Just a quck point to make.
    You could do everything in one script, but you may or may not want to have an individual script on each TV quad that plays its animation. And you could just reference and call their play functions from your remote script.

    So you could have a PlayAnimation() function in each TV script that you would just need to access from your remote script. So you'd have the main script attached to your remote with references to your TvOn and TvOff scripts which would be attached to their quads(gameobjects in the hierarchy/prefab), but you'd never have to really do anything else with them directly.

    I mean, write a "function PlayAnimation()" in each Tv script, then in the remote script, using Korno's example:

    Code (JavaScript):
    1. function TurnOnTheTV()
    2. {
    3. // Set your tvlight to active and your tvoff quad to inactive
    4. // call the tvlight's script to do its PlayAnimation() function
    5. // ex. TVLightScript.PlayAnimation()
    6. }
    BTW, if your TV animations are nothing more than turning the quads from black to white, or something simple like that, you could just change the quad's color instead of having two quads. If you are using a real TV show animation or something, disregard that.

    On the quad you could use the renderer.material.color = newcolor, for instance. If you want more info on that, you can let us know.

    Quick Edit: I think you could probably do a texture swap on a single quad, also through your script, but I don't know if that'd be very efficient and/or wise during play.