Using Type.GetType() with Unity objects

All of the following lines of code return null instead of the expected type.

Type.GetType("GameObject"); // should == typeof(GameObject)
Type.GetType("Renderer"); // should == typeof(Renderer)
Type.GetType("MeshRenderer"); // should == typeof(MeshRenderer)

Type.GetType("UnityEngine.GameObject"); // should == typeof(GameObject)
Type.GetType("UnityEngine.Renderer"); // should == typeof(Renderer)
Type.GetType("UnityEngine.MeshRenderer"); // should == typeof(MeshRenderer)

Help with this fix please. :)

1 Like

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:

GameObject obj = new GameObject();
Debug.Log(obj.GetType() == typeof(GameObject));
Debug.Log(obj.GetType().ToString());
Debug.Log (typeof(GameObject).ToString());
7 Likes

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.

I thought of a potential work-around using GetComponents() 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.

I know this is old but had the same issue - hopefully people will find this helpful:

//JS
import System;

function ConvertStringToType( inputString : String ) : Object
{
    return System.Activator.CreateInstance(Type.GetType(inputString));
}

so the following should return var type = typeof(GameObject):

var type : Object = ConvertStringToType( "GameObject" );
1 Like

The docs for System.Type.GetType say you need to use the assembly qualified name of the type. This means you need to include the name of the assembly after the full type name (including namespaces). So that would be:

Type.GetType("UnityEngine.GameObject, UnityEngine")

http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx

9 Likes

Actually, you don't need the Assembly name...This works:

Type myType1 = Type.GetType("ZotnipSequence.SeqMethodTranslator");
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"

    public class TypeUtility
    {
        public static Type GetTypeByName(string name)
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.Name == name)
                        return type;
                }
            }

            return null;
        }
    }

Mind you, if multiple type share the same name, it will only return the first found.

1 Like

It does not work for me.

1919624--123909--Rotator.cs (835 Bytes)

[quote=“Awesome_Dude”, post:9, topic: 476600]
It does not work for me.
[/quote]

No idea what that is, but you’re not calling “GetTypeByName” anywhere.

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"));

2 Likes

[quote=“Gibbonator”, post:6, topic: 476600]
The docs for System.Type.GetType say you need to use the assembly qualified name of the type. This means you need to include the name of the assembly after the full type name (including namespaces). So that would be:

Type.GetType("UnityEngine.GameObject, UnityEngine")

http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx
[/quote]

That’s a true answer, works, thx.

But how do I get the Type of my own classes?

typeof(MyOwnClass)

How could I get the Type of my own classes as a variable?
I have tags matching my class names.

for example:

string[] listOfTags = new string[] { "TagWhichIsAlsoAClassName", "Tag2" };

for (int i = 0; i < listOfTags.length;)
{
    GetComponent<typeof(listOfTags[i])>().enabled = true;
}
i++;

this does not work.