Im working on loading game data and therefor want to convert a string to a type so that I then can add the component. The problem is that it gives me a NullReferenceException: Object reference not set to an instance of an object: Code (CSharp): Type type = Type.GetType ("UnityEngine.UI.Text"); print (type.ToString()); UnityEngine.UI.Text is just a example for testing. I can't understand why it doesn't work so all help is welcomed! EDIT: "Text" doesn't work either
No EDIT: I accually have a enum named Type maybe that's why? EDIT 2: Changing to System.Type.GetType didn't do any difference
can you explain what you're trying to do. it looks like you're trying to get the text of a UI element.
I have a variable with a string that represents a class(component) in this example Text. I want to convert the string to a Type so I then can call the AddComponent(type);
so maybe something like this, sorry for code typos private string compString = "UnityEngine.Collider, UnityEngine"; var mytype = new Type.GetType(compString); gameObject.AddComponent(mytype);
string compString = "UnityEngine.UI.Text, UnityEngine"; var mytype = new Type.GetType(compString); gameObject.AddComponent(mytype); print(getComponent<Text>().text);
Yest that's kind of how I did it but it didn't work. It gives me the exception on this line: print (type.ToString()); So the type is for some reason null
Your script and UnityEngine.UI.Text end up in different assemblies. So you need to specify the assembly to look in. However I suspect te assembly name is still wrong. The UI is in a different assembly again to UnityEngine. Do some basic reflection on the type to figure out what the assembly its in is called.
Note that Type.GetType requires the assembly-qualified name of a type, unless it is in the currently executing assembly or something. See https://msdn.microsoft.com/en-us/library/w3f99sx1(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname(v=vs.110).aspx That's why you need to add ", UnityEngine" or whatever the assembly display name for the ui stuff is, or it will return null. Try to find out what the assembly name for the ui stuff is (I don't know anymore) and you should be able to get it to work
Type.GetType requires the assembly qualified name. You can check what that is from the .AssemblyQualifiedName property of a Type: Code (csharp): var text = typeof(UnityEngine.UI.Text); print (text.AssemblyQualifiedName); //prints "UnityEngine.UI.Text, UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" That's a mouthful, so you only want to use GetType if you've already stored that from earlier. If you want to get an arbitrary type by name, you'll either need to know which assembly it lives in, or search through all assemblies: Code (csharp): Type textType = null; string typeName = "UnityEngine.UI.Text"; var assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var assembly in assemblies) { textType = assembly.GetType(typeName); if (textType != null) break; } Debug.Log(textType); @johne5, if you're going to help people, try saying what you're doing, don't just throw code at them.
To save the type I use this: Code (CSharp): saveComponent.type = component.GetType().ToString (); And that saves into the file "UnityEngine.UI.Text" so shouldn't it be possible to access a component with that string?
You want GetType().AssemblyQualifiedName, as I did. C# needs the assembly to figure out where to look for the type. If you want to know why C# doesn't spit out AssemblyQualifiedName from ToString(), which would've been the convenient thing, call Microsoft.