- Home /
How to fix AddComponent(UnityEngine.GameObject, string, string)' is obsolete
string first = "Assets/X-UniTMX/Code/Map.cs (2684,5)";
string second = obj.GetPropertyAsString(Property_AddComponent + c);
UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, first, second);
I'm using a tiled loader (X_UniTMX) and it has some obsolete code which is preventing me to build the project.
I get this error :
Assets/X-UniTMX/Code/Map.cs(2685,63): error CS0619: `UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(UnityEngine.GameObject, string, string)' is obsolete: `Method is not meant to be used at runtime. Please, replace this call with GameObject.AddComponent<T>()/GameObject.AddComponent(Type).'
Any help would be appreciated.
Do what it tells you. I imagine this is detailed in the docs
Answer by lalaphoon · Jun 28, 2018 at 04:37 PM
I had the same issue. You have to get the type by System.Type.GetType(type)
and use gameObject.AddComponent( thatType)
.
Here is an example of how I fixed it.
public MegaModifier Add(string type)
{
/****************** The issue code **************************************
MegaModifier mod = (MegaModifier)UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, "Assets/Mega-Fiers/Scripts/MegaFiers/Modifiers/MegaModifiers.cs (614,36)", type);
if ( mod != null )
BuildList();
return mod;
*******************************************************/
//Bug Fixed for building on device
//This API is no longer being used.
var theType = System.Type.GetType(type);
if (theType == null || !(typeof(Component).IsAssignableFrom(theType)))
return null;
MegaModifier mod = (MegaModifier)gameObject.AddComponent(theType);
if (mod != null)
BuildList();
return mod;
}
reference : https://answers.unity.com/questions/1108811/addcomponent-changed-after-upgrading-unity.html
I am using $$anonymous$$egafiers too and got stuck at this step! I am so glad I find this answer. THANK YOU!!!!
Answer by Bunny83 · Feb 26, 2018 at 04:25 PM
Well, that framework used an internal API that was never meant to be used by any usercode. This method is / was part of Unity's automatic script updating service. This AddComponent method ist just a wrapper around the usual gameObject.AddComponent() but with a systematic search for the class type that is provided as string. It first checked if the type name specifies a type in the UnityEngine.dll. If no type was found it continued in the current assembly (which would include most of your own custom scripts) and if still no type was found it searches through all loaded assemblies.
Basically something like that:
using System.Linq;
// [ .. ]
static System.Type ResolveType(string aName)
{
System.Type result = typeof(Component).Assembly.GetTypes().Where(typeFromHandle.IsAssignableFrom).FirstOrDefault(t => (t.Name == aName|| t.FullName == aName);
if (result != null)
return result;
result = System.Assembly.GetCallingAssembly().GetType(aName);
if (result != null)
return result
result = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(a=>a.GetTypes()).SingleOrDefault(t=> t.Name == aName && typeof(Component).IsAssignableFrom(t));
return result;
}
Now you can replace those 3 lines with
string componentName = obj.GetPropertyAsString(Property_AddComponent + c);
var type = ResolveType(componentName);
if (type != null)
gameObject.AddComponent(type);
Note that all code here hasn't been syntax checked since I've directly written it here on UA.
Answer by Daemonhahn · Feb 26, 2018 at 04:26 PM
try changing
UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, first, second);
to tileObject.AddComponent();
where "tileObject" is the gameobject that you want to add component to, and "Type" is the type, such as
example
tileObject.AddComponent();
Are you saying the example should be tileObject.AddComponent(type) ; ?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Can't Build PC Standalone due to multiple namespace script errors but playable in editor 1 Answer
My android app build has more bugs on it than the editor/deveploment version 0 Answers
Can't build webgl project at all. 0 Answers
Multiple Cars not working 1 Answer