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.
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.