- Home /
function OnTriggerEnter --> if -->if --> Invoke... well, it doesn't work!
I have a function OnTriggerEnter.
Inside this function, there is an if statement and in it there is another if statement.
If the conditions of the 2nd if statement are met, I call an Invoke function which described later in the script.
Well, the invoke function is not invoked (It is supposed to change an object's tag and instantiate a gameObject but nothing happens)...
I've read somewhere that one cannot pass arguments to Invoke (actually I don't know what this means) and was wondering if that has anything to do with this use that I make.
Here is (part of) the script. I have 3 if statements. The 1st if statement has inside it 3 other if statements, the 3d of which tries to invoke the method toggleTagsETC.
function OnTriggerEnter (col: Collider) {
if (col.gameObject.tag == "Hot" || col.gameObject.tag == "WildCard"){ //if this if statement's conditions are met I call the toggleTagsETC function
Instantiate(DestroyedSound, transform.position, transform.rotation);
AnotherScript.ChainCharge +=1;
Destroy(gameObject);
if (Time.time-AnotherScript.lastDestructionTime <1){
Instantiate(chainSound, transform.position, transform.rotation);
}
AnotherScript.timeBetweenDestructions = (Time.time-AnotherScript.lastDestructionTime);
if (Time.time-AnotherScript.lastDestructionTime <1){
AnotherScript.chainCharger = (1-AnotherScript.timeBetweenDestructions)+AnotherScript.chainCharger;
}
AnotherScript.lastDestructionTime = Time.time;
if ((AnotherScript.chainCharger > chainTime) && (AnotherScript.frenzyOn== false)){
Invoke ("toggleTagsETC"); // this is the Invoke that does not get Invoked...
}
}
if (!(col.gameObject.tag in arrayOfGoodColliders)){ //this if statement doesnt't have anything to do with the problem
Instantiate(BadMoveSound, transform.position, transform.rotation);
timesWrongfullyMoved += 1;
}
if (timesWrongfullyMoved==3){ //this if statement doesnt't have anything to do with the problem
timesWrongfullyMoved=0;
Instantiate(invisibleBarrier, transform.position+Vector3(0,-2,0), transform.rotation);
Instantiate(invisibleBarrierOnSound, transform.position, transform.rotation);
renderer.material.mainTexture = barrierOnTexture;
Invoke ("RemoveBarrier", barrierEffectDuration);
}
}
function toggleTagsETC (){ // the method that should be invoked Instantiate(superBillboard, Vector3(-3.91,10.514,-0.182), Quaternion.identity); Instantiate(chainFullAnnouncementSound, transform.position, transform.rotation); Instantiate(FrenzyOnBillboard, Vector3(-2.126812,-0.002076507,4.278569), transform.rotation); cubeMovementIV.chainCharger = 0; cubeMovementIV.frenzyOn= true; cubeMovementIV.timeOfFrenzyOn = Time.time; frenzyCube.tag="FrenzyCube"; yield WaitForSeconds (cubeMovementIV.frenzyDuration); frenzyCube.tag="FrenzyCubeOff"; Instantiate(frenzyOnFalseSound, transform.position, transform.rotation);
If I replace
Invoke ("toggleTagsETC");
with
toggleTagsETC ();
everything inside "toggleTagsETC" is executed, until the line:
yield WaitForSeconds (cubeMovementIV.frenzyDuration);
Specifically, the following 2 lines are not executed:
frenzyCube.tag="FrenzyCubeOff";
Instantiate(frenzyOnFalseSound, transform.position, transform.rotation);
After I do as Mike suggested below in his kind comment:
function OnTriggerEnter (col: Collider) {
if (col.gameObject.tag == "Hot" || col.gameObject.tag == "WildCard"){ //if this if statement's conditions are met I call the toggleTagsETC function
Instantiate(DestroyedSound, transform.position, transform.rotation);
AnotherScript.ChainCharge +=1;
Destroy(gameObject);
if (Time.time-AnotherScript.lastDestructionTime <1){
Instantiate(chainSound, transform.position, transform.rotation);
}
AnotherScript.timeBetweenDestructions = (Time.time-AnotherScript.lastDestructionTime);
if (Time.time-AnotherScript.lastDestructionTime <1){
AnotherScript.chainCharger = (1-AnotherScript.timeBetweenDestructions)+AnotherScript.chainCharger;
}
AnotherScript.lastDestructionTime = Time.time;
if ((AnotherScript.chainCharger > chainTime) && (AnotherScript.frenzyOn== false)){
StartCoroutine("toggleTagsETC"); // this is the Invoke that works only until the "yield WaitForSeconds (5.0);" line
}
}
if (!(col.gameObject.tag in arrayOfGoodColliders)){ //this if statement doesnt't have anything to do with the problem
Instantiate(BadMoveSound, transform.position, transform.rotation);
timesWrongfullyMoved += 1;
}
if (timesWrongfullyMoved==3){ //this if statement doesnt't have anything to do with the problem
timesWrongfullyMoved=0;
Instantiate(invisibleBarrier, transform.position+Vector3(0,-2,0), transform.rotation);
Instantiate(invisibleBarrierOnSound, transform.position, transform.rotation);
renderer.material.mainTexture = barrierOnTexture;
Invoke ("RemoveBarrier", barrierEffectDuration);
}
}
function toggleTagsETC (): IEnumerator { // the method that works only until the "yield WaitForSeconds (5.0);" line Instantiate(superBillboard, Vector3(-3.91,10.514,-0.182), Quaternion.identity);//this is executed Instantiate(chainFullAnnouncementSound, transform.position, transform.rotation); //this is executed Instantiate(FrenzyOnBillboard, Vector3(-2.126812,-0.002076507,4.278569), transform.rotation); //this is executed AnotherScript.chainCharger = 0;//this is executed AnotherScript.frenzyOn= true;//this is executed AnotherScript.timeOfFrenzyOn = Time.time;//this is executed frenzyCube.tag="FrenzyCube";//this is executed yield WaitForSeconds (5.0);//this is NOT executed frenzyCube.tag="FrenzyCubeOff";//this is NOT executed Instantiate(frenzyOnFalseSound, transform.position, transform.rotation); //this is NOT executed }
the problem persists as shown in the commented lines above
I think you're going to need to show the code, the idea sounds ok, and the arguments to Invoke thing probably has nothing to do with it
Answer by Mike 3 · Jan 04, 2011 at 11:53 AM
Invoke doesn't work with coroutines - use StartCoroutine("toggleTagsETC"); if you want to invoke a coroutine by the name of the function instead of just calling it yourself
Secondly, I would also change
function toggleTagsETC (){
to
function toggleTagsETC () : IEnumerator {
so Unity knows for sure it's a coroutine. Try the first tip first though, in case that alone fixes it
What is cube$$anonymous$$ovementIV.frenzyDuration set to? Try print it before yielding
could you post the code again with using StartCoroutine and forcing the function to be IEnumerator, just to make sure everything is ok?
I think I finally got it o.O You're destroying the GameObject the coroutine is supposed to be running on, which means it'll never call the second half of that function
Answer by PrimeDerektive · Jan 04, 2011 at 01:41 PM
Put everything inside toggleTagsETC() inside:
while(true){
//code goes here
yield;
}
like this:
function toggleTagsETC (){
while(true){
Instantiate(superBillboard, Vector3(-3.91,10.514,-0.182), Quaternion.identity);
Instantiate(chainFullAnnouncementSound, transform.position, transform.rotation);
Instantiate(FrenzyOnBillboard, Vector3(-2.126812,-0.002076507,4.278569), transform.rotation);
cubeMovementIV.chainCharger = 0;
cubeMovementIV.frenzyOn= true;
cubeMovementIV.timeOfFrenzyOn = Time.time;
frenzyCube.tag="FrenzyCube";
yield WaitForSeconds (cubeMovementIV.frenzyDuration);
frenzyCube.tag="FrenzyCubeOff";
Instantiate(frenzyOnFalseSound, transform.position, transform.rotation);
yield;
}
}
And don't use invoke, just use toggleTagsETC(); Also, try to use CamelCase for method and function names, and camelCase for variables and properties.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093955im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get back player control? 0 Answers
Dynamic function calling 2 Answers
Call a function once from OnGUI 2 Answers
InvokeRepeating repeating too often with OnTriggerEnter 1 Answer