- Home /
how to stop a script for a while?
how to stop a script for a while? I wish that when the player passes a trigger, a script is blocked for a few seconds,but doesn't work
var timeOut = 3.0;
function Awake () { Invoke ("Enable", timeOut); }
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.tag == "Player") { GameObject.Find("messoplayer").GetComponent("input").enabled=false; GameObject.Find("Player").GetComponent("FPSWalker").enabled=false; GameObject.Find("Player").GetComponent("MouseLook").enabled=false; } }
function Enable () { GameObject.Find("messoplayer").GetComponent("input").enabled=true; GameObject.Find("Player").GetComponent("FPSWalker").enabled=true; GameObject.Find("Player").GetComponent("MouseLook").enabled=true; }
Answer by equalsequals · May 21, 2010 at 05:26 PM
Whenever I see a question involving "do x for t seconds" I always suggest using the MonoBehaviour Coroutine.
Do something like this:
void OnTriggerEnter(Collider other)
{
if(other.gameObject.name != "Player") return; //skips the rest of the function if something else entered the trigger besides what we want to actually trigger the event
StartCoroutine(MyCoroutine());
}
IEnumerator MyCoroutine()
{
//disable the desired script here
yield return new WaitForSeconds(10F);
//enable it here
}
That code will start a Coroutine when our Player enters the trigger. The Coroutine will then do something, wait 10 secs and do something else. Should get you the desired result.
Cheers,
==
p.s. I code in C#, obviously. If you need Javascript you could compare my script to the docs on Coroutines to port it over.
thank you.. I'm trying to change the script "acrivate trigger to activate it after X seconds", without success .. Here's the script
That script is probably a little overly complicated for what you most likely need. Judging by your request in your question, you have an FPSWalker script that when the object that the script is attached to enters a trigger, the script is temporarily disabled then re-enabled. Am I correct in my assumption?
I made this script .. now when I pass the trigger scripts are disabled, but then you do not enable more
Answer by Random Indie · May 21, 2010 at 05:15 PM
One way you could do it would be to either disabable the component by setting enable to false. You might have to do that from another script though as I'm not sure you can get a disabled script to re-enable itself. You can use the getcomponent function to obtain a reference to the necessary script.
You could also try encapsulating the code you want disabled in a big
if(!disabled) {...}
clause. You'd set the disabled flag in the trigger collision code given your example.
Hope that helps.
No, you can't get a disabled script to enable itself.
That's what I figured, wouldn't really be disabled otherwise :)
Thanks for confirmation, have't had time to make sure.
Answer by gianni123 · May 24, 2010 at 03:23 PM
disable script(WORK):
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.tag == "Player") { GameObject.Find("messoplayer").GetComponent("input").enabled=false; GameObject.Find("Player").GetComponent("FPSWalker").enabled=false; GameObject.Find("Player").GetComponent("MouseLook").enabled=false; print("HELLO"); } }
re-enable script(DOESN'T WORK)
var timeOut = 2.0; function OnTriggerEnter (collision : Collider) { if (collision.gameObject.tag == "Player") { print("hello1"); Invoke ("Activate", timeOut); } }
function Activate () {
GameObject.Find("messoplayer").GetComponent("input").enabled=true; GameObject.Find("Player").GetComponent("FPSWalker").enabled=true; GameObject.Find("Player").GetComponent("MouseLook").enabled=true; print("PROVA3"); }
Are you deactivating the script in which this all takes place in?
I disabled the script and it works. but the script that should resume does not work
I see you are using 2 scripts. You really do not need to. In your disable script paste your entire Activate function from the enable script and at the end of OnTriggerEnter in your disable script use the call to Invoke.