Search Unity

Send Float and use in other Script for Animation

Discussion in 'Scripting' started by makazin, Oct 21, 2014.

  1. makazin

    makazin

    Joined:
    Jun 20, 2014
    Posts:
    3
    Hello Guys,
    I will send this float too annother script and use the float for play an animation on Time.
    The first script name is "TimeOfDay" and this line i need in other script.

    Code (JavaScript):
    1. var Hour : float;
    The other script name is "DoorOpenClose".

    Code (JavaScript):
    1. private var todScript : TOD;
    2. var AtTime : float;
    3.  
    4. function Start() {
    5.  
    6. }
    7. function Update () {
    8.  
    9.     todScript = AtTime;
    10.     todScript = GetComponent(TOD).Hour;
    11.  
    12.     if(AtTime == 9)
    13.         AtTime = 9;
    14.         animation.Play("DoorOpen");
    15. }
    I will play animation if the time say 9 a.m or 10 a.m but in annother script.
    How too send and convert this float and use it for play animation at this time?

    I hope you can help me :)
     
    Last edited: Oct 21, 2014
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Code (CSharp):
    1. function Start() {
    2.     todScript = GetComponent(TOD);
    3. }
    4.  
    5. function Update(){
    6.     AtTime = todScript.Hour;
    7. }
    Something of that sort.
     
    makazin likes this.
  3. makazin

    makazin

    Joined:
    Jun 20, 2014
    Posts:
    3
    Thanks Guy. :)
    I have importet your script lines.

    Code (JavaScript):
    1. private var timer : int;
    2. var Door : GameObject;
    3. var DoorSound : AudioClip;
    4.  
    5. function Update() {
    6.  
    7.     timer = GetComponent(TOD).Hour;
    8.     isTimer = timer;
    9.  
    10.     if(isTimer == 9){
    11.     Door.animation.Play("DoorOpen");
    12.     audio.Play(DoorSound);
    13.     }
    14.     else {
    15.     Door.animation.Stop("DoorOpen");
    16.     audio.Stop(DoorSound);
    17.     }  
    18.     if(isTimer == 21){
    19.     Door.animation.Play("DoorClose");
    20.     audio.Play(DoorSound);
    21.     }
    22.     else {
    23.     Door.animation.Stop("DoorClose");
    24.     audio.Stop(DoorSound);
    25.     }
    26. }
    Is good work,
    but I had convert float in int...the animation not play on 9.999. He's play only on roundet Times 9 ^^
     
  4. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I'm glad it works for you.
    But a tip for the future - you should use "GetComponent" as little times as possible, so use it once in the start function.
    The reason being, GetComponent, Find, etc... Are somewhat slow, and if you use it every frame... Well, the more code that has it in the update, the slower the game will be, right?

    Anyway, I've modified your code a little, I hope it works, and works well for you, I am not a JS coder, you see, and I did not test that code (at work right now)


    Code (JavaScript):
    1.    
    2.     public var door : GameObject;
    3.     public var doorSound : AudioClip;
    4.     public var timeToOpen : float;
    5.     public var timeToClose : float;
    6.     private var timer : float;
    7.     private var todScript : TOD;
    8.     private var doorOpen : bool;
    9.    
    10.     function Start()
    11.     {
    12.         //Found and set once (cached)
    13.         todScript = GetComponent(TOD);
    14.        
    15.         //Set the animations to work only once (you can also set that in the animation editor, instead - disable "Loop Time" in the inspector)
    16.         Door.animation["DoorOpen"].wrapMode = WrapMode.Once;
    17.         Door.animation["DoorClose"].wrapMode = WrapMode.Once;
    18.     }
    19.    
    20.     function Update()
    21.     {
    22.         timer = todScript.Hour;
    23.        
    24.         OpenDoor();
    25.         CloseDoor();
    26.     }  
    27.    
    28.     private void OpenDoor()
    29.     {
    30.         //If the door is not open (as in, it has the value of false) AND the timer is above the float value of "timeToOpen", open the door
    31.         if(!doorOpen && timer > timeToOpen)
    32.         {
    33.             AnimateDoor("DoorOpen")
    34.         }
    35.     }
    36.    
    37.     private void CloseDoor()
    38.     {
    39.         //If the door is open (as in, it has the value of true) AND the timer is above the float value of "timeToClose", close the door
    40.         if(doorOpen && timer > timeToClose)
    41.         {
    42.             AnimateDoor("DoorClose")
    43.         }
    44.     }
    45.    
    46.     //This way, the code is written only ONCE, so if there is ever an error, or if you want to add something to the door animation sequence, you have to do it only ONCE, which means, easy maintenance and updates
    47.     private void AnimateDoor(String animationName)
    48.     {
    49.         Door.animation.Play(animationName);
    50.         audio.Play(DoorSound);
    51.     }
    52.  
     
    GarBenjamin likes this.