Changing Prefab's sprite doesn't work properly :(

Hello everyone,

I want to change my gameobjects’ sprites in my game. However, I cannot change the sprite of my object properly. It sounds odd but it only changes if I click to the prefab before I play the scene. If I click to the prefab and it can be seen in inspector, then it changes the sprite of the prefab.

I have several sprites for my objects to create a shop for my game. I check which ones are selected, then I try to apply these changes but it is not working. This is my first entry in this forum, so I could make a mistake while creating this post, so please forgive me if I had a mistake.

Here is my code,

for (int i = 0; i < shop.shopsText.Count; i++)
        {
            //if (bilgi.profileText.head == shop.shopsText[i].id)
            //{
            //    ChangeHead(shopImage[i+1]);
            //}
            if (bilgi.profileText.body == shop.shopsText[i].id)
            {
                ChangeBody(shopImage[i + 1]);
            }
            if (bilgi.profileText.bg == shop.shopsText[i].id)
            {
                ChangeBG(shopImage[i+1]);
            }
            if (bilgi.profileText.weapon == shop.shopsText[i].id)
            {
                 ChangeWeapon(shopImage[i+1]);
            }
        }
 void ChangeBody(Sprite img)
    {
        Head.GetComponent<SpriteRenderer>().sprite = img;
    }

    void ChangeWeapon(Sprite img)
    {
        Weapon.GetComponent<SpriteRenderer>().sprite = img;
    }

    void ChangeBG(Sprite img)
    {
        BGSkin.transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = img;
    }

If you’re changing the prefab, it won’t work on the build. In the editor it will actually change the asset on disk.

If you intend to change something that you Instantiate-d from a prefab, then use the reference returned from Instantiate<T>(); instead of the reference to the prefab.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

Unfortunately I didn’t understand the first part of your answer, howeever I got the idea of using a lot of dots. Thank you for the advice, appreciate it :slight_smile: