Why is the color of the player not changing properly?
I made a script to use the values of three sliders going from 0-255 to change the respective RGB values of the player sprite. I made the sliders go from the proper values, and I set the script up with what I think makes sense, but I'm not sure why it isn't working.
Here is the video showcasing the problem: https://youtu.be/_Z0zTGdsbxQ
And here is the code for the ColorSlider object, it holds all three sliders
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorSliders : MonoBehaviour
{
//Get sliders
[SerializeField] Slider redSlider;
[SerializeField] Slider greenSlider;
[SerializeField] Slider blueSlider;
//Get player
[SerializeField] GameObject player;
//Get player spriterenderer
SpriteRenderer playerSpriteRenderer;
private void Start()
{
playerSpriteRenderer = player.GetComponent<SpriteRenderer>();
redSlider.value = 255;
greenSlider.value = 255;
blueSlider.value = 255;
}
// Update is called once per frame
void Update()
{
Color tmp = new Color(redSlider.value, greenSlider.value, blueSlider.value);
tmp.r = redSlider.value;
tmp.g = greenSlider.value;
tmp.b = blueSlider.value;
player.GetComponent<SpriteRenderer>().color = tmp;
Debug.Log("Red value: " + tmp.r + "\nGreen value: " + tmp.g + "\nBlue value: " + tmp.b);
}
}
Any help or even code optimizations would be appreciated because I'm still decently new to Unity and C# and I'm struggling a little =) thank you
Answer by Danteb132 · Mar 24, 2021 at 07:13 PM
Also, the Debug.Log is printing out the right values, it just isn't changing them in the Player sprite even though everything is in update.
Your answer
![](https://koobas.hobune.stream/wayback/20220613031242im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnMouseEnter causes an object to disappear for some reason 1 Answer
Making a slider, slider does nothing even when attached to the script? 1 Answer
How to ignore specified color when changing sprite color? 0 Answers
Changing script variable from another script doesn't change it in the original script? 0 Answers
InverseLerp for vector3 1 Answer