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.
Examples:
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:
https://docs.unity3d.com/Manual/class-Halo.html
namespace UnityEngine internal sealed class Halo : Behaviour
Any solutions?
Can you provide the definition of the Hallo
class? Even if the class is sealed
, it should not be a problem. However, if the class is private
, then, yes, you can face this kind of issue.
it's the built in Halo class:
https://docs.unity3d.com/$$anonymous$$anual/class-Halo.html
namespace UnityEngine
internal sealed class Halo : Behaviour
Answer by Hellium · Mar 23, 2020 at 11:55 PM
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=0.0.0.0, Culture=neutral, PublicKeyToken=null").GetTypes();
Type haloType = types.First(t => t.Name.Equals("Halo"));
gameObject.AddComponent(haloType);
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.
Thanks, I think it may be a mistake since it was possible before they depreciated that function
Your answer
![](https://koobas.hobune.stream/wayback/20220612225541im_/https://answers.unity.com/themes/thub/images/avi.jpg)