- Home /
delayed function problem
I have 2 scripts attached to 2 different objects, when the first object becomes visible the second script whit my GUI is enabled, when the first object disappears, I want to close my GUI. The problem is that when the GUI is closed I can't see the animations of the buttons in it because there's no time to show them. This is my code, maybe it explains my problem better:
first script:
var otherScript : guifinal;
if (renderer.isVisible) {
otherScript.enabled = true;
}
else {
DisableRendererWithDelay();
}
}
function DisableRendererWithDelay() {
yield WaitForSeconds(2);
otherScript.enabled = false;
}
second script:
function OnEnable(){
iTween.ValueTo(gameObject,iTween.Hash("from",CurrentButtonSize(),"to",buttonVertSize,"easetype",iTween.EaseType.easeOutBack,"onupdate","ScaleButton","time",.5,"delay",.3));
iTween.ValueTo(gameObject,iTween.Hash("from",VertButtonSize(),"to",buttonOrizSize,"easetype",iTween.EaseType.easeOutBack,"onupdate","ScaleButton","time",.5,"delay",1));
}
function OnDisable(){
iTween.ValueTo(gameObject,iTween.Hash("from",CurrentButtonSize(),"to",buttonNormalSize,"easetype",iTween.EaseType.easeOutBack,"onupdate","ScaleButton","time",.5,"delay",.2));
}
There is a way to send instantly the trigger of "otherScript.enabled = true" but execute it after 2 seconds?
The problem I see with that is you do a yield WaitForSeconds(2) and then disable, so the OnDisable() in the second script gets called after 2 seconds.. You'll need to use a boolean in your second script that can be called before the yield, then that boolean triggers your tween before the object gets disabled.. See below
Answer by funkyllama · Dec 08, 2011 at 06:49 PM
You could try something like this ..
In your second script:
Add a boolean variable, something like 'disabling : boolean = false'
Then rename your OnDisable() function to Disable() and add disabling = false
function Disable(){
iTween.ValueTo(gameObject,iTween.Hash("from",CurrentButtonSize(),"to",buttonNormalSize,"easetype",iTween.EaseType.easeOutBack,"onupdate","ScaleButton","time",.5,"delay",.2));
disabling = false;
}
Add an if statement in the update function of your second script
if(disabling) {
disable();
}
In your first script:
just before you do your yield WaitForSeconds(2); add the line
otherScript.disabling = true;
Hope this makes sense :)
thank you so much, this was driving me mad, now is quite working (I have undestand the method, now I'm trying to apply correctly).. but I don't understand why also the OnEnable function starts after 2 seconds
Your answer
Follow this Question
Related Questions
Gui label delay 1 Answer
trying to delay a loop using yield - not working 1 Answer
How to WaitforSeconds a GuiButton ? 0 Answers
How to reload a gun with delay ? 2 Answers
Pause game while sound plays 1 Answer