SpriteRenderer not working (?)

I think i did the same as the tutorial to change the sprite color on the car, but it doesn’t change, can someone help me?

public class Delivery : MonoBehaviour
    {
    public bool hasPackage;
    [SerializeField] float DestroyDelay = 0.5f;
    [SerializeField] Color32 hasPackageColor = new Color32(1 , 1, 1, 1);
    [SerializeField] Color32 noPackageColor = new Color32(1 , 1, 1, 1);

    SpriteRenderer spriteRenderer;

    private void Start() 
        {
            spriteRenderer = GetComponent<SpriteRenderer>();
        }



    void OnCollisionEnter2D(Collision2D other) 
        {
            Debug.Log("aouch");
            
        }
    private void OnTriggerEnter2D(Collider2D other) 
        {
            
            if(other.tag == "Package" && !hasPackage){
                Debug.Log("package picked");  
                hasPackage = true;        
                spriteRenderer.color = hasPackageColor;
                Destroy(other.gameObject, DestroyDelay);
            }

    if (hasPackage == true){
            
            switch(other.tag){
                
                case "bCustomer":
                Debug.Log("Delivered to Blue");  
                
                hasPackage = false;
                break;

                case "rCustomer":
                Debug.Log("Delivered to Red");  
                hasPackage = false;
                break;

                case "oCustomer":
                Debug.Log("Delivered to Orange");  
                hasPackage = false;
                break;
                
            }
            spriteRenderer.color = noPackageColor;
            }

        }   
}
1 Like

The first thing I noticed here is that your colors are the same in code:

 [SerializeField] Color32 hasPackageColor = new Color32(1 , 1, 1, 1);
 [SerializeField] Color32 noPackageColor = new Color32(1 , 1, 1, 1);

Did you change them in the inspector?

If you did, is the following debug line displaying on your console?

Debug.Log("package picked");  

If that line isn’t displaying, then it has something to do with the tags and colliders, be sure that everything is set properly in the inspector.

Hope this helps.

in the inspector the colors are like in the video, green and white, and yes the debug is displaying that. And yet don’t understand why it doesn’t work, i’m probably blind or something :sweat_smile:

Oh! I get it :sweat_smile: silly me for not noticing this earlier.

Believe it or not! But the renderer is changing its color! But… it then goes back to its original color right after.

    if(other.tag == "Package" && !hasPackage)
    {
        hasPackage = true;        
    }

    if (hasPackage == true)
    { 
        spriteRenderer.color = noPackageColor;

See the issue? Right after you pick the package the hasPackage bool is set to true, so it runs the other if statement returning the renderer’s material color to its original state.

You can fix this very easily. Instead of having two if statements, set the second one to else, so it doesn’t run right after the other.

    if(other.tag == "Package" && !hasPackage)
    {
        hasPackage = true;        
    }
    else if (hasPackage == true)
    { 
        spriteRenderer.color = noPackageColor;
1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms