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.
var Hour : float;
The other script name is “DoorOpenClose”.
private var todScript : TOD;
var AtTime : float;
function Start() {
}
function Update () {
todScript = AtTime;
todScript = GetComponent(TOD).Hour;
if(AtTime == 9)
AtTime = 9;
animation.Play("DoorOpen");
}
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’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)
public var door : GameObject;
public var doorSound : AudioClip;
public var timeToOpen : float;
public var timeToClose : float;
private var timer : float;
private var todScript : TOD;
private var doorOpen : bool;
function Start()
{
//Found and set once (cached)
todScript = GetComponent(TOD);
//Set the animations to work only once (you can also set that in the animation editor, instead - disable "Loop Time" in the inspector)
Door.animation["DoorOpen"].wrapMode = WrapMode.Once;
Door.animation["DoorClose"].wrapMode = WrapMode.Once;
}
function Update()
{
timer = todScript.Hour;
OpenDoor();
CloseDoor();
}
private void OpenDoor()
{
//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
if(!doorOpen && timer > timeToOpen)
{
AnimateDoor("DoorOpen")
}
}
private void CloseDoor()
{
//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
if(doorOpen && timer > timeToClose)
{
AnimateDoor("DoorClose")
}
}
//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
private void AnimateDoor(String animationName)
{
Door.animation.Play(animationName);
audio.Play(DoorSound);
}