Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by schwertfisch · Jan 04, 2011 at 10:55 AM · functionontriggerenterinvoke

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jan 04, 2011 at 11:05 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jan 04, 2011 at 02:55 PM 1
Share

What is cube$$anonymous$$ovementIV.frenzyDuration set to? Try print it before yielding

avatar image PrimeDerektive · Jan 04, 2011 at 02:55 PM 1
Share

Weird... where is frenzyDuration defined?

avatar image Mike 3 · Jan 04, 2011 at 03:09 PM 1
Share

You're not getting errors in the console are you?

avatar image Mike 3 · Jan 04, 2011 at 03:19 PM 1
Share

could you post the code again with using StartCoroutine and forcing the function to be IEnumerator, just to make sure everything is ok?

avatar image Mike 3 · Jan 04, 2011 at 03:47 PM 1
Share

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

Show more comments
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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

Collider doesn't transfer to function 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges