Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using Type.GetType() with Unity objects

Discussion in 'Scripting' started by Zergling103, May 19, 2012.

  1. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    All of the following lines of code return null instead of the expected type.

    Code (csharp):
    1.  
    2. Type.GetType("GameObject"); // should == typeof(GameObject)
    3. Type.GetType("Renderer"); // should == typeof(Renderer)
    4. Type.GetType("MeshRenderer"); // should == typeof(MeshRenderer)
    5.  
    6. Type.GetType("UnityEngine.GameObject"); // should == typeof(GameObject)
    7. Type.GetType("UnityEngine.Renderer"); // should == typeof(Renderer)
    8. Type.GetType("UnityEngine.MeshRenderer"); // should == typeof(MeshRenderer)
    9.  
    Help with this fix please. :)
     
    Last edited: May 19, 2012
    hopetolive likes this.
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    As far as I can understand what you are asking.

    GetType returns a type. So Type.GetType() would return teh type of Type. so if Type does not have a type, then it will return null.

    Now, if you wanted to return the type of a GameObject you would use something like this:

    typeof(GameObject)

    if you wanted the type of an active object you would use something like this:

    gameObject.GetType

    Which returns... typeof(GameObject);

    These works fine:

    Code (csharp):
    1.  
    2. GameObject obj = new GameObject();
    3. Debug.Log(obj.GetType() == typeof(GameObject));
    4. Debug.Log(obj.GetType().ToString());
    5. Debug.Log (typeof(GameObject).ToString());
    6.  
     
  3. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    True, but that assumes certain things about what I am trying to do here. If that is something which would have worked in my scenario I would have thought of it already.
    What I am trying to do, however, involves the inspector and typing in the name of a class, so that System.Reflections functions like Type.GetField() and FieldInfo.SetValue() will work. Using GetComponent(String) worked fine for components but as far as I know this function will not return a reference to Renderers, nor the game object when "GameObject" is entered.

    Why I am trying to do this (not being the reason I posted) is to create a component which can arbitrarily modify any other component, game object, behavior, or what have you, in response to keystrokes (for example, switching weapons changes or activates several character components/game objects). Doing this is an alternative to creating a less flexible special-purpose behavior class (like WeaponSwitch) just for modifying a fixed collection of other behaviors or game objects in a similar way.
     
    Last edited: May 27, 2012
  4. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    I thought of a potential work-around using GetComponents<Component>() and then searching through the list for a match by using GetType().Name and a string comparison. Potentially not as fast but it'll do the job.
     
  5. bkovner100

    bkovner100

    Joined:
    Aug 5, 2012
    Posts:
    3
    I know this is old but had the same issue - hopefully people will find this helpful:

    Code (csharp):
    1. //JS
    2. import System;
    3.  
    4. function ConvertStringToType( inputString : String ) : Object
    5. {
    6.     return System.Activator.CreateInstance(Type.GetType(inputString));
    7. }
    so the following should return var type = typeof(GameObject):

    Code (csharp):
    1. var type : Object = ConvertStringToType( "GameObject" );
     
    t0m5000 likes this.
  6. Gibbonator

    Gibbonator

    Joined:
    Jul 27, 2012
    Posts:
    204
    igrus, TimPham, Eldoir and 6 others like this.
  7. nia1701

    nia1701

    Joined:
    Jun 8, 2012
    Posts:
    74
    Actually, you don't need the Assembly name...This works:
    Code (CSharp):
    1. Type myType1 = Type.GetType("ZotnipSequence.SeqMethodTranslator");
    2. Debug.Log("The full name is " + myType1.FullName);
    ZotnipSequence is a namespace.
    SeqMethodTranslator is the name of the class.
    If you wanted a sub class that lives inside SeqMethodTranslator you would adjust the string to be "ZotnipSequence.SeqMethodTranslator+SubClass"
     
  8. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Code (CSharp):
    1.     public class TypeUtility
    2.     {
    3.         public static Type GetTypeByName(string name)
    4.         {
    5.             foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
    6.             {
    7.                 foreach (Type type in assembly.GetTypes())
    8.                 {
    9.                     if (type.Name == name)
    10.                         return type;
    11.                 }
    12.             }
    13.  
    14.             return null;
    15.         }
    16.     }
    Mind you, if multiple type share the same name, it will only return the first found.
     
    spinaLcord likes this.
  9. Awesome_Dude

    Awesome_Dude

    Joined:
    Jan 13, 2015
    Posts:
    1
    It does not work for me.
     

    Attached Files:

  10. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    No idea what that is, but you're not calling "GetTypeByName" anywhere.
     
  11. chechoggomez

    chechoggomez

    Unity Technologies

    Joined:
    Feb 25, 2013
    Posts:
    91
    You need to use the full type name when calling Type.GetType(). Take a look at the following line:

    gameObject.AddComponent(System.Type.GetType("UnityEngine.Rigidbody, UnityEngine"));
     
    Eldoir and mitaywalle like this.
  12. bu213200

    bu213200

    Joined:
    Jun 10, 2014
    Posts:
    2
  13. col000r

    col000r

    Joined:
    Mar 27, 2008
    Posts:
    698
    But how do I get the Type of my own classes?
     
  14. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Code (csharp):
    1. typeof(MyOwnClass)
     
  15. owen_proto

    owen_proto

    Joined:
    Mar 18, 2018
    Posts:
    118
    How could I get the Type of my own classes as a variable?
    I have tags matching my class names.

    for example:
    Code (CSharp):
    1. string[] listOfTags = new string[] { "TagWhichIsAlsoAClassName", "Tag2" };
    2.  
    3. for (int i = 0; i < listOfTags.length;)
    4. {
    5.     GetComponent<typeof(listOfTags[i])>().enabled = true;
    6. }
    7. i++;
    this does not work.