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 MapuHoB · Dec 19, 2014 at 01:18 AM · c#methodinvoke

Is it possible to call a method which has optional arguments with Invoke?

 void Start()
     {
         this.GenerateLevel(true);
     }
 
     private void GenerateLevel(bool isFirst = false)
     {
         if (isFirst || this.IsTimeToGenerate())
         {
             this.currentLevel = Instantiate(Levels[Random.Range(0, this.Levels.Length)], this.GetNewPosition(isFirst), Quaternion.identity) as GameObject;
             this.currentLevel.transform.parent = this.transform;
             this.previousLevel = this.currentLevel;
         }
 
         Invoke("GenerateLevel", 0.5f);
     }


Is this code executable?

Comment
Add comment · Show 3
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 HYPERSAVV · Dec 19, 2014 at 01:57 AM 0
Share

EDIT: Removed comment, wrong information

avatar image MapuHoB · Dec 19, 2014 at 02:07 AM 0
Share

No, you are trying to invoke GenerateLevel(float) when you have GenerateLevel(bool). If you defined a second method GenerateLevel(float) then it would valid.

I don't try to invoke generateLevel with float. 0.5f (the second parameter I put in invoke) is for the time I want it to be executed

avatar image HYPERSAVV · Dec 19, 2014 at 02:22 AM 0
Share

@deadliness Correct, at work and mixed up Invoke and StartCoroutine in my head.

1 Reply

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

Answer by SkaredCreations · Dec 19, 2014 at 02:04 AM

No, you cannot use Invoke with methods that have parameters (optional or required), but you can still use coroutine that is more elegant in your case:

     void Start ()
     {
         StartCoroutine(GenerateLevel(true));
     }
     
     IEnumerator GenerateLevel(bool isFirst = false)
     {
         while (true)
         {
             if (isFirst || this.IsTimeToGenerate())
             {
                 this.currentLevel = Instantiate(Levels[Random.Range(0, this.Levels.Length)], this.GetNewPosition(isFirst), Quaternion.identity) as GameObject;
                 this.currentLevel.transform.parent = this.transform;
                 this.previousLevel = this.currentLevel;
             }
             yield return new WaitForSeconds(0.5f);
         }
     }
 
Comment
Add comment · Show 1 · 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 Bunny83 · Dec 19, 2014 at 09:04 AM 0
Share

Well, you don't really use isFirst in a meaningful way in this example since it won't change during the execution of the coroutine. This one would make more sense:

 IEnumerator GenerateLevel()
 {
     while (true)
     {
         this.currentLevel = Instantiate(Levels[Random.Range(0, this.Levels.Length)], this.GetNewPosition(isFirst), Quaternion.identity) as GameObject;
         this.currentLevel.transform.parent = this.transform;
         this.previousLevel = this.currentLevel;
         yield return null; // Just for safety in case "IsTimeToGenerate" never returns false in which case you wouldn't yield at all which would actually freeze the application.
         while (!IsTimeToGenerate())
             yield return new WaitForSeconds(0.5f);
     }
 }

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

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I use something like Invoke() to call a method in another class? 1 Answer

Invoking methods cross-class with arguments 1 Answer

My function doesn't appears in the OnClick button script 5 Answers


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