• 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 Pflegeleichtt · Apr 13, 2011 at 12:57 PM · stringtypegetcomponentinchildren

convert string to type?

Hey, i want to modify my unity editor, so i write a script to add components with their name. You can write the name in a textfield an then it adds the component with the name to den game object. But then i would add a component to the childrens, but this doesn't work. error : No appropriate version of 'UnityEngine.Component.GetComponentInChildren' for the argument list '(String)' was found.

here is my script code:

enter code here

function OnGUI() { newcomponent = EditorGUI.TextField(Rect(10,25,position.width - 20, 20), "Component:", newcomponent); if(Selection.activeTransform) if(GUI.Button(Rect(0, 50, position.width, 30), "Add Component")) for(var t : Transform in Selection.transforms) t.gameObject.AddComponent(newcomponent); if(GUI.Button(Rect(0, 85, position.width, 30), "Remove Component")) for(var t : Transform in Selection.transforms) if(t.GetComponent(newcomponent) != null){ DestroyImmediate(t.GetComponent(newcomponent)); } else{ if(t.GetComponentInChildren(newcomponent) != null){ DestroyImmediate(t.GetComponentInChildren(newcomponent)); } } if(GUI.Button(Rect(0, 120, position.width, 30), "Deactivate")) for(var t : Transform in Selection.transforms) if(t.GetComponent(newcomponent) != null){ t.GetComponent(newcomponent).enabled=false; } else{ if(t.GetComponentInChildren(newcomponent) != null){ t.GetComponentInChildren(newcomponent).enabled=false; } } if(GUI.Button(Rect(0, 155, position.width, 30), "Activate")) for(var t : Transform in Selection.transforms) if(t.GetComponent(newcomponent) != null){ t.GetComponent(newcomponent).enabled=true; } else{ if(t.GetComponentInChildren(newcomponent) != null){ t.GetComponentInChildren(newcomponent).enabled=true; } }

enter code here

I think i need a function like parsetype or something. Does anyone have a solution for this problem?

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 yoyo · Apr 13, 2011 at 08:34 PM 0
Share

Wouldn't it be a lot less work and almost as user friendly to use the AddComponent$$anonymous$$enu attribute on your components so that the user gets a nicely grouped set of components to choose from in the menu? For details see http://unity3d.com/support/documentation/ScriptReference/AddComponent$$anonymous$$enu.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TowerOfBricks · Apr 13, 2011 at 02:28 PM

I think the only way to do this is by using reflection. Namely Assembly.GetType (string);

import System.Reflection;

function WhatsTheType (myType : string) { Debug.Log ("This was a "+Assembly.GetExecutingAssembly.GetType (myType)); }

I hope the code will compile, used to writing C#. And also, I think the GetExecutingAssembly call is quite slow, so you might want to save that between the tests.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

C# string as type for AddComponent 2 Answers

What's the difference between GetComponent and GetComponent(StringName)? 1 Answer

How Can I Change GetType() And GetField() And GetValue() ? 2 Answers

GUI password check question 2 Answers

GetComponent via String name? 3 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