- Home /
Change material when an event occurs
Building a demo for my 3d platformer/racing game and when a player reaches a certain point or object (like a portal that spawns enemies), i'd like to change some of the surrounding object's materials. for example i have a lot of light-blue neon that should change into red neon, i don't need to change all materials, just those of the objects in range so i went with Physics.OverlapSphere and it triggers the surrounding objects, returns all Debug.Logs but the materials still aren't swapped.
No compiling errors.
public class InfectedPortal : MonoBehaviour {
public int virusCount;
public GameObject barrier;
public GameObject enemy;
public GameObject portalOpens;
public GameObject activePortal;
public GameObject spawningEffect;
Material mat;
public Material redNeon;
Renderer rend;
Rotater rot;
private void Start()
{
rot = GetComponent<Rotater>();
StartCoroutine(Spawn());
}
private IEnumerator Spawn () {
Debug.Log("started");
Collider[] hitColliders = Physics.OverlapSphere(transform.position, 100);
foreach (Collider c in hitColliders)
{
Debug.Log("checking array");
rend = c.GetComponent<Renderer>();
Debug.Log(rend);
if (rend != null)
{
mat = rend.material;
Debug.Log(mat.name);
if (mat.name == "Neon Cyan (Instance)")
{
Debug.Log("About to change color")
mat = redNeon;
}
}
}
//After this it's activating & deactivating particle effects etc so no point to include that here
}
}
Hey Danor, it seems to me your material shader islikely the same for boththe red and the blue neon aside from the actual colour(color) changes. This might ins$$anonymous$$d suggest an approach to changing the color values of the material you already have, however, the first restriction is that all other objects with that material will be affected also... So, in order to change your material you must set the renderer.material property of that material to equal another material but this will affect all objects using that material...right :) So what you can do in th eifrst instance is to make two gameobject prefabs, one of the object with a red material and one of the object with a blue material. these prefabs will be how you can change the surrounding area to have a different colour based on locality... The real question comes from taking this approach. How do you get the gameobjects to change based on a locality driven approach to triggering these changes off? Alas, not an answer but an offer to be by your side whilst you jump head first down the rabbit hole :) Cheers bud G
Answer by pako · Jul 04, 2017 at 06:19 PM
Instead of:
mat = redNeon;
You should do:
rend.material = redNeon;
...to change the material on the renderer.
Thanks mate!
For anyone who might need this, i also had to change: if (mat.name == "Neon Cyan (Instance)") to if (rend.material.name == "Neon Cyan (Instance)")
and got rid of "mat"
Answer by znSawyer · Jul 04, 2017 at 06:18 PM
Is Renderer a component of Collider?
Will GetComponent work to find siblings? I think try to get to the GameObject of the Colldier before getting the renderer.
something like
rend = c.transofrm.parent.GetComponent();
You might need to check rend for null before this point.
@znSawyer no need to use the GameObject.GetComponent version. The Component.GetComponent vesrion, such as Renderer.GetComponent works just fine, as it "Returns the component with name type if the game object has one attached, null if it doesn't":
https://docs.unity3d.com/ScriptReference/Component.GetComponent.html
Your answer
Follow this Question
Related Questions
change material on object when touched. 2 Answers
Changing two different objects renderer colour 1 Answer
How to change a value in custom shader through script, C# 1 Answer
Changing material color creates a memory leak 1 Answer
What keyword string should I add to enable/disable when changing shader properties ? 1 Answer