- Home /
AddComponent changed after upgrading Unity
I've just upgraded from Unity 4 to 5.
It has changed a few lines of code. But the newly generated code is givin warnings.
Let me first show the original code, which worked perfectly fine.
void Start () {
a1 = gameObject.AddComponent(PlayerPrefs.GetString ("animal1") + "Functions") as IAnimalFunctions;
currentAnimal = a1;
Animal1Shape();
}
I tried to change the code back to this, but it gave me the following error:
Assets/Scripts/Animals/AnimalContainer.cs(21,25): error CS0619:
UnityEngine.GameObject.AddComponent(string)' is obsolete:
GameObject.AddComponent with string argument has been deprecated. Use GameObject.AddComponent() instead. (UnityUpgradable).'
After upgrading, the above was changed into the following.
a1 = UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, "Assets/Scripts/Animals/AnimalContainer.cs (16,8)", PlayerPrefs.GetString ("animal1") + "Functions") as IAnimalFunctions;
And this gives me a warning:
[Assets/Scripts/Animals/AnimalContainer.cs (16,8)] Component type 'SheepFunctions' found on caller assembly. Consider replacing the call method call with: AddComponent()UnityEngine.Debug:LogWarningFormat(String, Object[]) UnityEngineInternal.APIUpdaterRuntimeServices:ResolveType(String, Assembly, String) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineInternal/APIUpdaterRuntimeServices.cs:45) UnityEngineInternal.APIUpdaterRuntimeServices:AddComponent(GameObject, String, String) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineInternal/APIUpdaterRuntimeServices.cs:27) AnimalContainer:Start() (at Assets/Scripts/Animals/AnimalContainer.cs:16)
The PlayerPrefs.GetString ("animal1") + "Functions" is equal to "SheepFunctions" which is another class.
How do I fix this problem? :)
By judging the source you've given us, it seems like the way you've structured your classes may not be the best way. Have you tried adding the Functions component and then changing it based on the animal?
Also, here's a similar problem: http://answers.unity3d.com/questions/254479/getcomponent-from-string-name.html
Hope this helps
It was working pretty fine before I updated. I'd like to do it this way still. Why can't I just define a type by a string value anymore?
Answer by steakpinball · Dec 07, 2015 at 09:23 PM
This may help. http://answers.unity3d.com/answers/923278/view.html
Safe way:
if ("SheepFunctions" == PlayerPrefs.GetString ("animal1") + "Functions") {
gameObject.AddComponent<SheepFunctions>();
}
Unsafe way:
gameObject.AddComponent(System.Type.GetType(PlayerPrefs.GetString ("animal1") + "Functions"));
Your "unsafe way" is not more unsafe than the old AddComponent string version. You might want to do:
var type = System.Type.GetType(PlayerPrefs.GetString ("animal1") + "Functions");
if (type != null && typeof(Component).IsAssignableFrom(type))
gameObject.AddComponent(type);
This is the safest way and the equivalent of the old string version. You can use an extension method like this:
public static class GameObjectExtension
{
public static Component AddComponentByString(this GameObject aGameObject, string aTypeName)
{
var type = System.Type.GetType(aTypeName);
if (type == null || !typeof(Component).IsAssignableFrom(type))
return null;
return aGameObject.AddComponent(type);
}
}
With that you can simply use "AddComponentByString" just like the old string version:
gameObject.AddComponentByString(PlayerPrefs.GetString ("animal1") + "Functions");
Since the old AddComponent method that takes a string does still exist (at least in Unity version 5.2.1) I used a different name for the extension method.
I can't really get this to work.
gameObject.AddComponent(type) as IAnimalFunctions;
Gives me the following error:
error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Never$$anonymous$$d I fixed it, thanks. Great help.