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 /
avatar image
0
Question by shamblinwithshotguns · Feb 23, 2018 at 06:04 PM · stringfunctionspropertiesarguments

Is it posible to use a single function to change any property plugged in to it?

So I have a small "shop" that allows the player to increase or decrease their workforce. I am trying to use a combination of buttons to enable the player to do so, but I don't want to write multiple functions when one will suffice.

This is, essentially what I am trying to simplify:

 public void AddToProperty1()
 {
        player.properties.Property1++;
 }
 
 public void AddToProperty2()
 {
        player.properties.Property2++;
 }
 // and so on...

Ideally, it would look something like...

 public void AddToProperty(string property)
 {
        player.properties.property++;
 }

But this code doesn't work, something is missing or this is not how I communicate to the computer what I am trying to do. Is it possible to simplify the function in this way?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Lilius · Feb 23, 2018 at 07:22 PM

You could add switch inside your method:

     int property1;
     int property2;
 
     public void AddToProperty(string property)
     {
         switch (property)
         {
             case "property1":
                 property1++;
                     break;
             case "property2":
                 property2++;
                 break;
         }
     }
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
avatar image
1

Answer by homer_3 · Feb 23, 2018 at 08:10 PM

You could store your properties in an array and use an enum to ID each one, then pass an enum into the increment function like

 enum PropertyEnum { PROP1, PROP2, PROP3};
 
 List<int> properties;
 
 public void AddToProperty(PropertyEnum propId)
 {
   properties[(int)propId]++;
 }
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
avatar image
0

Answer by TheAltbacker · Feb 23, 2018 at 08:58 PM

You could also overload 'AddToProperty' so whenever you call it, it does slightly different things based on parameters and return type. You could pass in a reference to your property, so you'd always be changing the one currently passed in.

 public void AddToProperty(ref float _floatProperty)
 {
      _floatProperty++;
 }
 
 public void AddToProperty(ref int _intProperty)
 {
       _intProperty++;
 }
 //etc.

Haven't tested this, so it might not work exactly how I imagine, but I do think it's a viable idea.

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

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

C# - parsed strings not changing in function 1 Answer

String Arrays in a function 0 Answers

Using a string to call a function from another script without "SendMessage" 2 Answers

Making an in-game command line. How do I get arguments? 1 Answer

Get Object, Trigger Attached Method,Trigger method on collider script 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