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 castor · Aug 19, 2013 at 05:17 AM · editorstringclassname

Show variable name from a class as String

So i have this class:

 class ActorKnowledge {
     var state : boolean;
     
     function ActorKnowledge (state : boolean) {
         this.state = state;
     }
     
     function ActorKnowledge (state : boolean, selectedList :  List.<ActorKnowledge>) {
         this.state = state;
         selectedList.Add(this);
     }
     
 }

And its used in several Lists. I need to have them display in the editor from reading the Lists that contain them but I can't convert the class name to string. This is what i have so far:

     for (var playerKnowledge : ActorKnowledge in scriptPlayer.playerKnowledgeList){
          var knowledgeName = playerKnowledge.ToString();
                    
         if (!playerKnowledge.state){
             if ( GUI.Button (Rect (leftPadding, row01PositionY, position.width - buttonPaddingRight, buttonHeigth), (knowledgeName)) ){
                 if(EditorApplication.isPlaying){
                     knowledgeManager.GetKnowledge (playerKnowledge);
                 }
             }
             row01PositionY += buttonPadding;
         }
         else {
             EditorGUI.LabelField(Rect(entryLeftPadding,row01PositionY,position.width,buttonHeigth), knowledgeName);
             row01PositionY += buttonPadding;
         }
     }

What I really want to figure out is the variable knowledgeName! In the current example where I do a ToString() I just end up getting the button/label called ActorKnowledge.

Any help would be amazing!

UPDATE:

SO I think I phrased this incorrectly. What I meant is, I want the name of the variable that was created using this class, so for example I have these:

         knowsAboutPocketWatch = new ActorKnowledge(false, playerKnowledgeList);
         knowsAboutJournal = new ActorKnowledge(false, playerKnowledgeList);
         canProveSarahInnocence = new ActorKnowledge(false, playerKnowledgeList);

and I would like them to show in the editor with their names rather than as "ActorKnowledge"

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 DaveA · Aug 19, 2013 at 05:19 AM 0
Share

I don't understand. You say you want the class name. The class is Actor$$anonymous$$nowledge. ToString give you "Actor$$anonymous$$nowledge" What were you hoping for?

avatar image castor · Aug 19, 2013 at 06:04 AM 0
Share

You are right, I didn't phrase it correctly. What I meant is that I want the name of the variable that was made using that class.

avatar image DaveA · Aug 19, 2013 at 06:25 AM 0
Share

Can you say why you want this? Normally you just have a var at the top of your script and it shows in the editor with the name of that var. You can set that via Inspector or programatically.

avatar image castor · Aug 19, 2013 at 06:34 AM 0
Share

Each one of those variables can be changed by the player during gameplay and I need a Custom ui where i can turn them on and off for fast debugging. Right now it ends up looking like this:

alt text

$$anonymous$$aking it impossible to know what each actor$$anonymous$$nowledge actually is. The only solution I can think of is to add another var to the class (e.g. name : String) and whenever I instantiate a new Actor$$anonymous$$nowledge I would also add the name to it, something like this:

 class Actor$$anonymous$$nowledge {
     var name : String;
     var state : boolean;
 
     function Actor$$anonymous$$nowledge (state : boolean, name : String, selectedList :  List.<Actor$$anonymous$$nowledge>) {
         this.name = name
         this.state = state;
         selectedList.Add(this);
     }   
 }

 knowsAboutPocketWatch = new Actor$$anonymous$$nowledge(false, "knowsAboutPocketWatch" player$$anonymous$$nowledgeList);

and then use the player$$anonymous$$nowledge.name for display in the editor

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by flamy · Aug 19, 2013 at 05:31 AM

one simple solution is using

 this.GetType().Name;
 
 objectName.GetType().Name;


But remember that it wont work if it is a static class.

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 MasterLG · Aug 19, 2013 at 05:23 AM

I'm not sure if JavaScript has typeof, but have you tried typeof(ClassInQuestion).ToString()

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 castor · Oct 24, 2013 at 05:58 PM 0
Share

Yeah, both those options dont work. ToString still shows the type of object.

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

18 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

Related Questions

Naming array elements in editor 3 Answers

[JS] [Editor] SerializedObject from a custom class. 0 Answers

Converting String to Variable Name 3 Answers

Adding To Array From Editor 3 Answers

Update List<> in editor 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