0

If you try creating test scene, with a simple script to change sprite every a period of time.

The sprite doesn't change, but if you try to log the changed sprite name, you'll see it has changed successfully. Which means only the visual is not changing.

Testing on Unity 6000.0.3f1, and on android S24 Ultra

My TestSpriteRendererChange.cs script

using System.Collections;
using UnityEngine;

public class TestSpriteRendererChange : MonoBehaviour
{
    [SerializeField] private SpriteRenderer spriteRenderer;
    [SerializeField] private Sprite[] sprites;

    private void Start()
    {
        StartCoroutine(ChangeSprites());
    }

    private IEnumerator ChangeSprites()
    {
        var index = 0;

        foreach (var sprite in sprites)
        {
            Debug.Log($"before sprite: {spriteRenderer.sprite.name}");

            spriteRenderer.sprite = sprite;

            Debug.Log($"after sprite: {spriteRenderer.sprite.name}");

            yield return new WaitForSeconds(0.1f);

            index++;
        }

        if (index == sprites.Length)
        {
            index = 0;

            StopCoroutine(nameof(ChangeSprites));
            StartCoroutine(nameof(ChangeSprites));
        }
    }
}

Behaviour in unity editor

enter image description here

Behaviour in android device

enter image description here

This issue is related to URP 2D only, it works just fine in built-in-RP 2D projects.

4
  • Well. One consideration is i thought android was 30fps so 0.1s would be 3 frames. The fact its changing shape but not texture is curious but the graphics capabilities are considerably less. Do you get any additional messages in the player log files Commented May 27, 2024 at 16:47
  • @BugFinder Timing is not the issue, I increased it to 1f which is one second, and the issue is the same Commented May 27, 2024 at 18:16
  • Ok but as it went unmentioned it was a good place to start. Having to remake the component seems a big hammer and while of course it does reset that sounds like something you should report to unity. Commented May 27, 2024 at 18:51
  • Yes I filed a report to unity Commented May 27, 2024 at 20:21

1 Answer 1

0

This was an actual bug in unity 6000.0.3f1, so I did the following

As a workaround for this issue, I've modified my script as follow :-

I changed my TestSpriteRendererChange.cs to the following :-

using System.Collections;
using UnityEngine;
 
public class TestSpriteRendererChange : MonoBehaviour
{
    [SerializeField] private GameObject spriteGO;
    [SerializeField] private Sprite[] sprites;
 
    private void Start()
    {
        StartCoroutine(ChangeSprites());
    }
 
    private IEnumerator ChangeSprites()
    {
        var index = 0;
 
        foreach (var sprite in sprites)
        {
            // check if SpriteRenderer exists
            var hasComponent = spriteGO.TryGetComponent(out SpriteRenderer sr);
 
            // we could cash reference to sortingLayer and sortingOrder to avoid reset
            if (hasComponent)
            {
                // we remove the component if exists
                Destroy(sr);
            }
 
            // wait for end of frame to avoid null reference error
            yield return new WaitForEndOfFrame();
 
            // we add the SpriteRenderer component again
            var spriteRenderer = spriteGO.AddComponent<SpriteRenderer>();
 
            spriteRenderer.sprite = sprite;
 
            Debug.Log($"after sprite: {spriteRenderer.sprite.name}");
 
            yield return new WaitForSeconds(1f);
 
            index++;
        }
 
        if (index == sprites.Length)
        {
            index = 0;
 
            StopCoroutine(nameof(ChangeSprites));
            StartCoroutine(nameof(ChangeSprites));
        }
    }
}

By removing the SpriteRenderer component and adding it again, resolves the issue.

Let me know if there is a better solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.