Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Fir3stormStudio · Aug 05, 2015 at 08:08 AM · c#functioncall

call function with same name + number

So I have 30 different functions all like this

 public void level23()
     {
         theGame.levelSelected = 23;
     }

This is what is called to setup the next level in game. I was wondering if there was a way for me to call this in a retry button by using something like this with theGame.levelSelected being an int

 level + theGame.levelSelected();

this would save a rather long switch.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by allenallenallen · Aug 05, 2015 at 08:33 AM

Well, first of all, you shouldn't have 30 different functions to change a single integer. Why not literally just change the integer?

Can you not type this every time you need to load a new level?

 theGame.levelSelected = 23;

And if anything, you can do this:

 public void LevelThing(int num){
    theGame.levelSelected = num;
    // Then do other things here if you want to.
 }

Comment
Add comment · Show 8 · 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 Fir3stormStudio · Aug 05, 2015 at 08:46 AM 0
Share

there is a lot more that goes into that level function i just gave the basic code thats needed for this solution. It sets all the variables on the enemies as well as the goals etc. for the level

avatar image allenallenallen · Aug 05, 2015 at 08:50 AM 1
Share

And all of that can be handled in the same long void, you know?

Look at this; https://unity3d.com/learn/tutorials/modules/beginner/scripting/switch

 public void LevelThing(int num){
     theGame.levelSelected = num;
 
     switch (num){
         case 1:
            // Do all your level 1 specific set ups here.
            break;
         case 2:
            // Do all your level 2 stuff here
            break;
         // And so on.
     }
 
 }

avatar image Fir3stormStudio · Aug 05, 2015 at 09:01 AM 0
Share

how would you go about setting theGame.levelSelected when you click on the level, as currently when you click on a level it runs the appropriate function

avatar image allenallenallen · Aug 05, 2015 at 09:43 AM 1
Share

I'm guessing you didn't instantiate the buttons. If you did that you can just assign the number from an array to each of them.

If you can't instantiate the buttons for some reason, you can attach a script to each button and give itself an integer.

avatar image Cherno · Aug 05, 2015 at 10:12 AM 0
Share

To answer the question directly (although I agree with allenallenallen that there seem to be ways to make it a lot more efficient): You can use Send$$anonymous$$essage to call a function by it's name as a string. This string can of course be whatever you like, such as "Your$$anonymous$$ethodName" + 23.ToString().

Show more comments

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

25 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

Related Questions

List.add() not working when function called by 2nd script 1 Answer

How can I call a method from another script? 3 Answers

Why is calling a function wrong? 2 Answers

Check if a function is no longer being called? 3 Answers

Multiple Cars not working 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