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 Blink123123 · Mar 29, 2013 at 08:31 AM · stringclassreflectionconvert

Convert String To Class

I have a String CharName and i want:

 CharName CharStats = (CharStats)Player.tranform.Find("Mesh").GameObject.GetComponent("PlayerController") as CharStats;

The problem is I can't Declare CharName CharStats since CharName is a string. But CharName contains the name of a class (script). Let's say the script is named TestScript the line should become:

 TestScript CharStats = (TestScript)Player.transform.Find ("Mesh").GameObject.GetComponent("PlayerController") as TestScript;

So my question how to convert the String CharName to be used as the class TestScript.

Thank You :)

I am currently using the switch case method but if I will continue the code will become VERY long so I'm sure there is a better way to do this without making a switch case with 100 cases.

Comment
Add comment · Show 1
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 whydoidoit · Mar 29, 2013 at 11:06 AM 0
Share

Ok so I don't know what you are actually trying to accomplish - which I guess I need to because you can't do what you suggest there. There are a number of techniques that could be applied depending on the context and for sure you don't want to do it in a switch statement!

You have choices like:

  • Send$$anonymous$$essage

  • Interfaces

  • Inheritance

Let me understand what you are trying to achieve

1 Reply

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

Answer by Blink123123 · Mar 29, 2013 at 11:16 AM

Solved It ! To Call The Value Use This (In Case Someone Else Wants Help):

 (float)CharStats.GetField ("VariableName").GetValue(typeof(float));
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

11 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

Related Questions

Multiple Cars not working 1 Answer

How to call a variable using a String for the variable name? 1 Answer

GUI set max amount of characters for Label 1 Answer

Generate Json from Class Type List 1 Answer

How to convert a string to int array in Unity C# 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