Changing color via script is not working
I'm testing a mechanic using just a color change. I try to change the color value of a Sprite Renderer, when I check with the debug.log the values are correct but is not showing in the game view. Why is that hapening?
Here the code... very simple I guess:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour {
public int puntajeNivel2;
public int puntajeNivel3;
public int puntajeNivelFin;
GameObject cuerpo;
GameObject cam;
int puntaje;
Color persColor;
// Use this for initialization
void Start () {
cuerpo = GameObject.Find ("Cuerpo");
persColor = cuerpo.GetComponent<SpriteRenderer> ();
cam = GameObject.Find ("Main Camera");
}
void Level2 (){
persColor = Color.red;
}
void Level3 (){
persColor = Color.green;
}
void LevelFin (){
persColor = Color.blue;
}
// Update is called once per frame
void Update () {
puntaje = cam.GetComponent<Puntuacion> ().puntuacion; //revisando
Debug.Log (persColor);
if (puntaje >= puntajeNivel2 && puntaje < puntajeNivel3){
Level2 ();
}
if (puntaje >= puntajeNivel3 && puntaje < puntajeNivelFin){
Level3 ();
}
if (puntaje > puntajeNivelFin){
puntaje = puntajeNivelFin;
LevelFin ();
}
}
}
Answer by Hellium · Apr 23, 2018 at 01:31 PM
Because you are chaning a Color, not the color of the sprite renderer. Because Color
is a struct
(value type), persColor = cuerpo.GetComponent<SpriteRenderer> ().color;
creates a copy of the renderer's color.
void Level2 (){
persColor = Color.red;
cuerpo.GetComponent<SpriteRenderer> ().color = persColor ;
}
void Level3 (){
persColor = Color.green;
cuerpo.GetComponent<SpriteRenderer> ().color = persColor ;
}
void LevelFin (){
persColor = Color.blue;
cuerpo.GetComponent<SpriteRenderer> ().color = persColor ;
}
Your answer
Follow this Question
Related Questions
Image Colour change not working with anythign other than base preset colours 2 Answers
How to change the color of my text object? 1 Answer
Changing Color of Object Using a Timer 1 Answer
Is there any way to make a gradient within unity?,Is there any way to make a gradient? 1 Answer
How can I change material color smoothly with C# like in Magnets Game? 1 Answer