- Home /
How Do You Change Two Materials Of One Object, And Then Change Them Back?
I'm trying to change the two materials of my object when the mouse hovers over it, and then change them back once the mouse stops hovering over it. Absolutely nothing happens when I play the game so help would be nice. My code is here: using UnityEngine;
public class Build : MonoBehaviour
{
public Material hoverColorGrass;
public Material hoverColorGround;
private Renderer rend;
public Material startMaterialGrass;
public Material startMaterialGround;
void Start()
{
rend = GetComponent<Renderer>();
}
void Update()
{
void OnMouseEnter()
{
rend.materials[0] = hoverColorGrass;
rend.materials[1] = hoverColorGround;
}
void OnMouseExit()
{
rend.materials[0] = startMaterialGrass;
rend.materials[1] = startMaterialGround;
}
}
}
Answer by Isaac_Marovitz · Apr 21, 2019 at 02:11 PM
Remove OnMouseEnter() and OnMosueExit() from your Update() function
using UnityEngine;
public class Build : MonoBehaviour
{
public Material hoverColorGrass;
public Material hoverColorGround;
private Renderer rend;
public Material startMaterialGrass;
public Material startMaterialGround;
void Start()
{
rend = GetComponent<Renderer>();
}
void OnMouseEnter()
{
rend.materials[0] = hoverColorGrass;
rend.materials[1] = hoverColorGround;
}
void OnMouseExit()
{
rend.materials[0] = startMaterialGrass;
rend.materials[1] = startMaterialGround;
}
}
}
@Isaac_$$anonymous$$arovitz I tried what you said and it still didn't make a difference
Your answer
Follow this Question
Related Questions
Altering Material through Script Causes Transparent Rendering Error - C# 1 Answer
Render objects with different materials when switching between two cameras in HDRP 0 Answers
Prevent material from rendering behind itself 0 Answers
Shader errors in Windows build ( renders black ) but not in Editor 2 Answers