Hi all, I’m new to unity so sorry if this was asked before. I’ll try my best to explain the issue
So in my program there are toggles that are ‘seats’. When a seat toggle is on, it displays an ‘empty’ sprite. When it is clicked, the toggle is off and it displays an ‘occupied’ sprite. I assigned temperature values as floats to each seat toggle which changes as a seat goes from empty to occupied and vise versa.
I use a spriteRenderer to change the sprites’ colour as the temperature of their seats change. So for exampe, if a seat’s temperature is between 22-23, the sprite is rendered blue, when it is 23-24, it is green, etc.
The issue is that at runtime, the actual sprite image doesn’t change colour even when the renderer’s colour in inspector changes. As you can see below, the seat 1A’s renderer changes to red in inspector, which means the code works fine (i guess?) even the debug log indicates that the code were run through, but the sprite remains its default light yellow.
unity doesn’t give any errors. The one in screenshot is because I’m only testing with seat 1A so other seats aren’t as completed
using UnityEngine;
using UnityEngine.UI;
public class assignSeat : MonoBehaviour
{
public Toggle seat; //creates seat toggle object
public float temp; //creates a publically viewable temperature variable
SpriteRenderer sprite;
public Color changeable;
void Start()
{
temp = Random.Range(22.0f, 26.5f); //random cabin temperature
sprite = GetComponent<SpriteRenderer>();
sprite.color = changeable;
}
/*void Update()
{
if (temp > 22 && temp < 25) //if temperature is withing this range, a bluish sprite is displayed
{
Debug.Log("change img!!");
seat.image.color = new Color(1, 0, 0, 1);
}
}*/
public void ChangeToggle() //public function to detect toggle state
{
//Debug.Log(Toggle.isOn);
if (seat.isOn == true) //if toggle on, it displays empty seat image meaning seat is empty
{
Debug.Log(seat.name + " empty!!");
temp = Random.Range(22.0f, 26.5f);
if (temp > 22 && temp < 24) //if temperature is withing this range, a red sprite is displayed
{
Debug.Log("change img!!");
sprite.color = new Color(1, 0, 0, 1);
}
if (temp > 24 && temp < 26.5f)
{
Debug.Log("change img aa!!");
sprite.color = new Color(0, 0, 0, 1);
}
}
else if (seat.isOn == false)
{
Debug.Log(seat.name + " occupied!!"); //if off, occupied image is displayed meaning seat is occupied
temp = Random.Range(35.0f, 38.0f); //random passenger temperature
}
}
}
Any help would be very appreciated!!!