Friday, October 28, 2022
HomeGame Developmentunity - Sprite renderer would not replace colour in realtime

unity – Sprite renderer would not replace colour in realtime


I am unable to breed this difficulty. Utilizing the next script:

public class CollisionColor : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D different) {
        if (different.collider.TryGetComponent(out SpriteRenderer otherSR)) {
            GetComponent<SpriteRenderer>().colour += otherSR.colour;
        }
    }
}

And this sprite setup:

Inspector setup of sprite

I discover the sprites change color in play mode once they collide, as anticipated:

Animation of coloured balls changing colours on collision

So, the issue is someplace in code you haven’t proven us. Double-check that the thing you are inspecting is the one you are seeing on display, and that your sr variable is referring to the SpriteRenderer you count on. A standard error right here is to maintain holding onto a reference to the prefab the objects have been spawned from, quite than the stay copy working within the scene.

Should you’re unable to establish the trigger, then edit your query to incorporate a Minimal Full Verifiable Instance: each line of code and each step of scene/inspector setup {that a} reader would wish to comply with, ranging from a brand new, empty challenge, to breed the identical drawback you are seeing. As soon as we are able to reproduce the difficulty, we are able to check potential fixes to make sure they’re going to be just right for you.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments