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
2
Question by BDX777 · Jan 30, 2015 at 09:35 PM · getcomponentstringtype

GetComponent via String name?

Hi, I'm trying to GetComponent with a String name. Is there a way to convert a string to a Type identifier?

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
3

Answer by Drigomen · Mar 15, 2017 at 09:19 AM

I'm trying to GetComponent with a String name

Easy, you can use: GetComponent(string type). From the oficial docs: https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

 using UnityEngine;
 
 public class GetComponentExample : MonoBehaviour
     {
     public const string knownClassName = "Rigidbody";
     public string unknownClassName;

     void Start( )
     {
         Rigidbody comp1 = gameObject.GetComponent(knownClassName ) as Rigidbody ;
         var comp2 = gameObject.GetComponent(unknownClassName ) as MonoBehaviour;
     }
 }

Is there a way to convert a string to a Type identifier?

Yes. As @smoggach said, it depends on the language. In c#, it sometimes work: System.Type.GetType("yourStringHere")

But, I don't know why you would want this. If you don't know the type at compile time, you don't have other option than cast it as a MonoBehaviour, and you can't do much with that...

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 smoggach · Jan 30, 2015 at 09:46 PM

This depends more on the language you're using.

C#: https://msdn.microsoft.com/en-us/library/aa310400%28v=vs.71%29.aspx

In java you would use the method Class.forName().

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 Rispat-Momit · Mar 14, 2017 at 11:28 PM

After spending a few hours searching, I found the solution. Here is a small script that I made :)

This will create an array of Strings where you can write the names of the scripts you want to activate.

 var MyScriptNames: String[];
 
     function Start(){
     
     for(var i : int = 0; i < MyScriptNames.Length; i++)
         {
          (gameObject.GetComponent(MyScriptNames[i]) as MonoBehaviour).enabled = true;
          } 
     }
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

23 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

Related Questions

GetComponent and converting string to Type type 1 Answer

Passing a Script Name to a Function 2 Answers

Can i give GetComponent a variabel instead of a scriptname? 1 Answer

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

How Can I Change GetType() And GetField() And GetValue() ? 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