Search code examples
c#unity-game-enginetexturestexture2d

Unity2D - messed up sprite while accesing it from script


I have 64x64 texture of tree :

enter image description here

but output gives me this (black messed object is my tree)

enter image description here

If i drag sprite into editor, its rendered as meant to be(with new gameobject created). But im accesing it through script :

public Sprite treesIcon;

SpriteRenderer sr;

TileTypeHandler typeHandler;

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


void Update () {
    switch(typeHandler.tileType) /// nevermind enum, its edited
    {
        case TileType.Woods:
            {
                sr.sprite = treesIcon;
                break;
            }
    }
}

point is to dynamically change textures.

Can anyone explain me this behaviour? Is there somethin I am missing about sprites and textures? How can i fix it?


Solution

  • The result looks like the SpriteRenderer is using the wrong shader, change the Material to Sprites-Default, and it should work.

    Would you have colored images, you immediately would see that there is something wrong, because they would also appear black.