Cambiar material haciendo clic en Unity3D
– UnityAssets3Free
hola , soy kein y para hoy os traigo
nueva pregunta curiosa
Estoy tratando de cambiar el material representado en un cubo al hacer clic. Simple si otra condición (creo). yo intenté
void Update()
if (Input.GetMouseButtonDown(0))
Debug.Log("Click");
if (GetComponent<Renderer>().material == brick)
Debug.Log("Brick");
GetComponent<Renderer>().material = normal;
else if(GetComponent<Renderer>().material == normal)
Debug.Log("Normal");
GetComponent<Renderer>().material = brick;
Estoy entrando en el primer ciclo, eso es GetMouseButtonDown
. Pero nada le sucede al cubo después de eso. sin errores ni advertencias.
1 respuesta 1
prefiero usar uno bool
bandera en lugar de verificar la referencia del material
// Set the initial value via the Inspector
// to decide whether this should start as brick or normal
[SerializeField] private bool isBrick;
// If possible already drag in the renderer via the Inspector
[SerializeField] private Renderer _renderer;
// If ever needed later let others know directly whether this is a brick
// this property allows read-only acccess
public bool IsBrick => isBrick;
private void Awake ()
// As fallback get the renderer ONCE on runtime
if(!_renderer) _renderer = GetComponent<Renderer>();
// Apply the initial state
_renderer.material = isBrick ? brick : normal;
void Update()
if (Input.GetMouseButtonDown(0))
Debug.Log("Click");
Debug.Log(isBrick ? "Brick" : "Normal");
// Invert the bool flag
isBrick = !isBrick;
// Apply the new state
_renderer.material = isBrick ? brick : normal;
nota: si aun no se resuelve tu pregunta por favor dejar un comentario y pronto lo podremos de nuevo , muchas gracias
sin mas,hasta la proxima