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 KureKureciCZ · Feb 03, 2017 at 09:34 PM · c#scripting problemstringint

Accesing specific int from inspector

A have a lot of ints

 int thing1;
 int thing2;
 int thing3;
 int thing4;
 int thing5;
 int SelectedInt;
 public string IntToSelect;

If i write the ints name in the strings field in the inspector, it should do this:

  SelectedInt = IntToSelect;

Or something similar. But, is there a way, WITHOUT having tons of ifs for every single one of them? Thanks.

Comment
Add comment · Show 4
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 · Feb 03, 2017 at 09:34 PM 0
Share

You need C# reflection

https://www.tutorialspoint.com/csharp/csharp_reflection.htm

avatar image KureKureciCZ Hellium · Feb 04, 2017 at 12:40 PM 0
Share

I cant quite get how it works, can you provide a example code? @Hellium Thanks

avatar image Hellium KureKureciCZ · Feb 04, 2017 at 12:48 PM 0
Share

I have never used it myself, but you will find posts with examples like this one : https://forum.unity3d.com/threads/access-variable-by-string-name.42487/

Show more comments

2 Replies

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

Answer by Hellium · Feb 04, 2017 at 06:23 PM

Make sure your integers are declared public and do the following :

 using System.Reflection;

 // ...
 public int thing1;
 public int thing2;
 public int thing3;
 public int thing4;
 public int thing5;

 public int GetIntValue( string integerName )
 {
     System.Type type = GetType();
     FieldInfo info = type.GetField(integerName );
     if( info == null )
     {
         throw new System.Exception( "The field " + integerName + " does not exist" );
     }
     return (int)info.GetValue(this);
 }

Use the [HideInInspector] attribute if you don't want to see the variables in the inspector

EDIT :

To get private fields :

     FieldInfo info = type.GetField(value,BindingFlags.NonPublic | BindingFlags.Instance);
Comment
Add comment · Show 4 · 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 KureKureciCZ · Feb 04, 2017 at 06:43 PM 0
Share

Am i really that dumb?

[1]: /storage/temp/87372-screenshot.png

screenshot.png (9.2 kB)
avatar image Hellium KureKureciCZ · Feb 04, 2017 at 06:50 PM 0
Share

I forgot to indicate that you must add using System.Reflection at the top of your file.

AND :

  THATValue = GetIntValue( "thing1"  ) ;
avatar image KureKureciCZ Hellium · Feb 04, 2017 at 08:08 PM 0
Share

I did add using System.Reflection; BUT this is still not working:alt text

BTW REALLY sorry for your time, I really appreciate it ;) Thank you

screenshot.png (7.7 kB)
Show more comments
avatar image
0

Answer by KureKureciCZ · Feb 04, 2017 at 08:17 PM

Here is the whole code for everyone:

 using UnityEngine;
 using System.Collections;
 using System.Reflection;
 
 public class YOURCLASSNAME : MonoBehaviour {
     public string ToSelect;
     public int Selected;
     public int thing1;
     public int thing2;
     public int thing3;
     public int thing4;
     public int thing5;
     public int GetIntValue(string integerName)
     {
         System.Type type = GetType();
         FieldInfo info = type.GetField(integerName);
         if (info == null)
         {
             throw new System.Exception("The field " + integerName + " does not exist");
         }
         return (int)info.GetValue(this);
     }
     void Start() {
         Selected = GetIntValue(ToSelect);
         Debug.Log(Selected);
     }
 }
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

8 People are following this question.

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

Related Questions

checking numbers in strings 2 Answers

Cannot implicitly convert type `string' to `int' 0 Answers

trying to get an Array string to read an Int 0 Answers

Error FormatException: Input string was not in a correct format. 0 Answers

How to combine up to 3 and perform an action 2 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