Halo AddComponent problems

How do I add a Halo component, the Halo class is sealed and AddComponent(“Halo”) is deprecated.
I know you can use a prefab, but I need to be able to do it programmatically.


Behaviour halo = gameObject.AddComponent<Halo>();

Error: ‘Halo’ Inaccessible due to
protection level

Behaviour halo2 = (Behaviour)gameObject.AddComponent("Halo");

Error: “GameObject.AddComponent with
string argument has been deprecated.
Use GameObject.AddComponent()
instead. (UnityUpgradable).”

edit: it’s the built in Halo class:

namespace UnityEngine
internal sealed class Halo : Behaviour

Any solutions?

The following solution uses Reflection.

Often, when the classes are internal / private, it’s because there is a good reason. You should handle the following with care.

// At the top of the script
using System.Linq;
using System.Reflection;

// In your function
var types = Assembly.Load("UnityEngine.CoreModule, Version=, Culture=neutral, PublicKeyToken=null").GetTypes();
Type haloType = types.First(t => t.Name.Equals("Halo"));

I haven’t been able to dig deeper and change the value of the fields size and color at runtime, even with reflection. Not sure it is possible.