Reputation: 166
I have an object that changes its color when it collides with another object, and decreases its size: gameObject.transform.localScale /= 2;
but it has a white halo
.
I want the halo
to match my object's color. So when the object is a green color, so also the halo
will be green as well. And if my object is blue, then the halo
will be blue as well. Also, I want that when my object detect collision with other object, halo
also decrease and I don't know how can I do it.
Code change color (blue, red or green) when I press the screen:
public class ChangeColor : MonoBehaviour {
public Material[] materials;
public Renderer rend;
private int index = 1;
// Use this for initialization
void Start () {
rend = GetComponent<Renderer> ();
rend.enabled = true;
}
public void Update() {
if (materials.Length == 0) {
return;
}
if (Input.GetMouseButtonDown (0)) {
index += 1;
if (index == materials.Length + 1) {
index = 1;
}
print (index);
rend.sharedMaterial = materials [index - 1];
}
}
}
I know use halo
but programatically I don't know.
Upvotes: 1
Views: 857
Reputation: 59
This Halo Component is accessible with a bit of work, let me demonstrate it with code.
Private void Start() {
SerializedObject haloComponent = new SerializedObject(this.gameObject.GetComponent("Halo"));
haloComponent?.FindProperty("m_Color").colorValue = Color.Red;
}
There are some more things you can do but this is the way to get a reference to the Halo.
Keep in mind GetComponent<>
is searching for something that is Halo and GetComponent("Halo")
is searching for something with the name Halo. Since the Component is named Halo it works like a charm. Try it out
Upvotes: 0