- Home /
My function isn't ending but is being repeatedly being called from the update function
Hi,
I'm creating an animation sequence which I want to be set to active when a camera is set to active.
But I can't work out how to fix this - when I play this the update is calling my function continuously: what do I look into so that I only call the function once?
#pragma strict
var cameraPortacabin : GameObject;
var text1 : GameObject;
var text2 : GameObject;
var text3 : GameObject;
var screen1 : GameObject;
var screen2 : GameObject;
function Start () {
text1.SetActive(false);
text2.SetActive(false);
text3.SetActive(false);
screen1.SetActive(false);
screen2.SetActive(false);
}
function Update () {
if (cameraPortacabin.active == true) {
portacabinSequence();
}
}
function portacabinSequence () {
yield WaitForSeconds (1.5);
screen1.SetActive(true);
yield WaitForSeconds (2);
text1.SetActive(true);
yield WaitForSeconds (5);
text1.SetActive(false);
text2.SetActive(true);
yield WaitForSeconds (4);
text2.SetActive(false);
screen1.SetActive(false);
screen2.SetActive(true);
text3.SetActive(true);
yield WaitForSeconds (3);
text3.SetActive(false);
screen2.SetActive(false);
screen1.SetActive(true);
yield;
}
(Sorry for the newb question, I'm just getting so many errors thrown when I'm trying to fix this. I can't work out if I should be calling it once from the update function, or if I should stop the coroutine replaying from inside the function)
Best, Laurien
Wow that works perfectly thank you (if you put your comment into an answer I can tick it as answered)
Why is it better to call the function directly from the script which enables the camera object?
This code would work, but isn't as clean as it could be. Unfortunately UnityScript doesn't support events, which would be useful in a situation such as this, to notify other scripts that the camera object has been disabled.
There may also be some solutions that work with UnityScript, for example this article could be helpful in explaining a bit about events. Either way, there's lots of different approaches to solving the same problem :)
Answer by ByteSheep · May 09, 2015 at 05:35 PM
If I understand correctly, the problem is that the portacabinSequence function is being called multiple times when the camera object is set active.
Perhaps the easiest way of modifying your current code would be to add a variable that holds the last known state of the camera object, e.g.:
// ... other variables etc
var lastKnownState : Boolean = true; // set default to false if camera starts disabled
function Update ()
{
if (cameraPortacabin.active != lastKnownState)
{
lastKnownState = cameraPortacabin.active;
if (lastKnownState) // same as if (lastKnownState == true)
portacabinSequence();
}
}
(Not tested, but something along those lines could work.) However, it would perhaps be better to call the portacabinSequence function directly from the script which enables the camera object. You could use SendMessage() or access the script through a reference to do this, e.g.
MyAnimationScript.portacabinSequence();