How to get "Halo" component and set his size and color via script

Hi, I’m having a problem with Halos in unity3d v. 2022.3.4f1. In particular, I would like to be able to change their "size" and their "color" via C# script.

I tried several solutions including the following:

//To get the Halo:
Halos = new List<SerializedObject>();
childs = temp.GetComponentsInChildren<Transform>();
for (int i = 0; i < childs.Length; i++)
{
    if (childs[i].GetComponent("Halo") != null)
        Halos.Add(childs[i].GetComponent("Halo"));
}
//To Set the size and the color if you want:
Halos[i].FindProperty("m_Size").floatValue = Intensity;
Halos[i].ApplyModifiedProperties();

Now. in the editor that works properly but I know that SerializedObject works only in the editor and when i try to build the project it fails. Does anyone know how to fix it and make it works even in the final build?

Since the Halo component is a legacy component and not fully exposed to scripting, you might face some limitations. However, I suggest using a different approach to achieve a similar effect. You could use a Light component with a custom script to control its range and color. Here’s an example:

Create a new C# script called “CustomLightController” and paste the following code:

using UnityEngine;

public class CustomLightController : MonoBehaviour
{
    public Light controlledLight;
    public float intensity = 1f;
    public Color lightColor = Color.white;

    void Update()
    {
        controlledLight.range = intensity;
        controlledLight.color = lightColor;
    }
}

Attach this script to a GameObject with a Light component and assign the Light component to the “Controlled Light” field in the CustomLightController script. You can now change the intensity and color of the light through the script.

This approach should work in both the editor and the final build.

Creating halo effect is so simple to the point that you can easily create your very own solution in minutes and let go of this dinosaur already.

  1. Grab a billboard shader off the internetz (Simple Billboard shader for Unity · GitHub)
  2. Modify it’s code to your needs
Halo.shader

Halo.shader

Shader "Unlit/Halo"
{
	Properties
	{
		[MainTexture][NoScaleOffset] _MainTex ("Texture", 2D) = "white" {}
		[MainColor][HDR] _Color ("Color", Color) = (1,1,1,1)
	}
	SubShader
	{
		Tags{ "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }

		ZWrite Off
		Blend One One

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog

			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 pos : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _Color;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv.xy;

				// billboard mesh towards camera
				float3 vpos = mul((float3x3)unity_ObjectToWorld, v.vertex.xyz);
				float4 worldCoord = float4(unity_ObjectToWorld._m03, unity_ObjectToWorld._m13, unity_ObjectToWorld._m23, 1);
				float4 viewPos = mul(UNITY_MATRIX_V, worldCoord) + float4(vpos, 0);
				float4 outPos = mul(UNITY_MATRIX_P, viewPos);

				o.pos = outPos;

				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				col *= _Color;
				// apply fog
				UNITY_APPLY_FOG(i.fogCoord, col);
				return col;
			}
			ENDCG
		}
	}
}
  1. Create material using this shader
  2. Add a Mesh Filter & Mesh Renderer to render a quad mesh using this material

Note: Use Shader Graph if you can as you will probably need to implement mesh instancing for performance eventually and Shader Graph makes that a check box.