Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by losingisfun · Sep 11, 2016 at 08:31 AM · functionsparsingarguments

C# - Can a public function argument be a selection of public static variables?

Just a quick question to know whether or not this is possible.

I like to keep the habit of being DRY with my coding, so i don't like to recreate the same function, just to change one variable name. For example, I have a lot of buttons, and each one does the same basic function but for different GameObjects, example:

 public void pressedSoundOptionButton() {
     if (soundsOption) {
         soundsOptionMarker.SetActive (false);
     } else if (!soundsOption) {
         soundsOptionMarker.SetActive (true);
     }

I have buttons that attach to these public functions, But what i'd like is that each button can use the same function, but pass an argument telling it which variable and GameObject to check for. (Maybe two arguments)

IS it possible to do something like:

     public void pressedOptionButton (bool.name BooleanToUse) {
 //do stuff with the boolean variable given to this function
 }

Obviously i can't do (string varName), But I could always pass a string, and then lay out a long list of if's saying if (string == "var1") { booleanTouse = var1; But i feel like that would be bad practice.

(Also I'm new to C#) Whats the genius way of doing this?

Comment
Add comment · Show 2
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 Hellium · Sep 11, 2016 at 08:37 AM 0
Share

This is not a Unity question, but a pure C#-related one, and thus, does not belong to Unity Answers.

One word to answer you though : Reflection

http://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp

FAQ :

What are the guidelines for writing good questions?

We want Unity Answers to become a comprehensive database of questions and answers related to Unity.

avatar image losingisfun Hellium · Sep 11, 2016 at 09:02 AM 0
Share

well perhaps it could be a unity question, since I'm parsing the arguments through the unity button/inspector menu, and this could be related if Unity has some - if any - way to parse those arguments in a unique way, like a drop down menu to select variables within that script to be parsed. (But granted, i see that this is unlikely, and probably just a C# question - doesn't hurt to ask though)

0 Replies

· Add your reply
  • Sort: 

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

53 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 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

error CS8025: Parsing error: dont know what to do here 1 Answer

How to remotely (with a different C# script or js) activate function from the main C# script? 1 Answer

Is it possible to make custom automatically-called methods like Start and Update? 0 Answers

How to pass a game object through to a function? 2 Answers

How can i reverse a function by doing the same action again?,How do I revert a transformation after previously transforming it? 0 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