I am trying to use the following code:
function OnTriggerEnter (other : Collider)
{
if(other.collider.gameObject.tag == "FRAME") {
(GameObject.Find("WATERSPAWN").GetComponent("Instantiate_Random_Sec_RandomObject_WATERSPAWN")as Behaviour ).enabled = false;
Debug.Log(" Disable ");
}
}
function OnTriggerExit ( other : Collider)
{
if(other.collider.gameObject.tag == "FRAME") {
(GameObject.Find("WATERSPAWN").GetComponent("Instantiate_Random_Sec_RandomObject_WATERSPAWN")as Behaviour ).enabled = true;
Debug.Log(" Disable ");
}
}
To enable / disable this:
var OBJECT : Rigidbody[];
var minWait = 1.0;
var maxWait = 10.0;
function Start ()
{
while(true)
{
yield WaitForSeconds(Random.Range(minWait, maxWait));
var OBJECT : Rigidbody = Instantiate(OBJECT[Random.Range(0,OBJECT.Length)], transform.position, Quaternion.identity);
}
}
Any idea why this is not working?
Thanks,
Greg
Have you tried Destroy on that component?
Thanks, zazz0000, but I don’t want to permanently destroy that component, just disable it until my object has left the
collider trigger. I wonder if Start on the component that I’m looking to disable is preventing this from working?
Thanks,
G
Is there another way to temporarily disable a script other than what I’m attempting to do with the above code?
This works, but apparently not with function Start.
Any ideas?
I am interested to know the answer to this. Typically, what I do is use a ‘paused’ state and have a standard base class that supports ‘isPaused’ and is used by everything. Like to know if what you’re after gets solved. If you solve it, please post the answer.
Gigiwoo.
You could put a bool in the script, flip its state to enable disable it. Put scripts contents inside an if statement that only runs when bool is true.
Disabling a component does not stop the coroutines it started (the yield statement means you’re using a coroutine). You can stop the coroutine by deactivating the gameObject, but activating it later won’t restart the coroutine. You’re better off making a dedicated function for it and using StartCoroutine and StopCoroutine or StopAllCoroutines.
Or you can switch to an Update-based approach, in which case enabling and disabling does work.
Yes, coroutine is the answer. Thanks for your help!
-G