Sprites not rendered by SpriteRenderer

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!!!

from your screenshot your sprite is “none”, therefore there is no sprite to display

1 Like

Yep, for some reason your sprite is empty. There is something else though, the default colour of the (empty) sprite has its alpha set to 0. You need to add a sprite and set the colour alpha to 255.

The code, on the other hand, is correct. :slight_smile:

1 Like

hi, since a toggle has 2 sprites, which one should I add for the empty slot in the renderer? Unity doesn’t allow me to use 2 renderers. Btw the empty sprite is only in 1A’s renderer. For background and checkmark they each have an individual renderer with their sprites attached with full alpha
7144541--854810--upload_2021-5-16_10-23-48.png
so the seat 1A object is like this, just in case you want to see:
7144541--854813--upload_2021-5-16_10-24-53.png

it will be better if you show screenshot of the renderer thats not working as intnded

I’m not sure that I understand what you are asking; it’s up to you to know what you want. If the toggle has 2 positions, you can:

  • leave the default sprite (square, circle, etc.) and replace it by script,
  • define a default sprite of you own and replace it by the other one by script.

:slight_smile:

wow I figured it out…im such a noob lol

so I was playing with SpriteRenderer all this time and ignored that ‘Image’ above it…just then I noticed that at runtime I only coded the sprite renderer to change but not the image…

so i changed
‘SpriteRenderer sprite’ and
‘sprite = GetComponent();’
to ‘Image sprite’ and ‘sprite = GetComponent();’

and wow it works now


looks like the SpriteRenderer is useless now

anyways thank you a lot for the advices!