Changing sprite on prefab works for multiple scenes in Editor, but not on Android

Hi all!

I have a little problem with a game I’m working on. I am trying to change the sprite property of a SpriteRenderer on a prefab, so that the player can choose different avatars in one scene and use the same avatar in other scenes. My code to change the sprite and update the prefab and instance works fine in all scenes in the editor, but when I build to Android the sprite reverts to the default prefab sprite when I go to a new scene. The code I am using to change the sprites is a switch statement, and the prefab, instance, and new sprite variables are all assigned in the inspector:

switch (avatarNumber) {
	case 0:
		if (CheckAvatarUnlockStatus(avatarNumber)) {
			avatarCount++;
			avatarSpriteRendererPrefab.sprite = newSprite; // update the scene
			avatarSpriteRendererInstance.sprite = newSprite; // update the instance
		}
		else {
			SetQuestionMarkAvatar(avatarNumber);
		}
		break;

Is there some trick for getting changes to a prefab to carry over between scenes on Android? :face_with_spiral_eyes:

Thanks so much!

Store the sprite to e.g. static variable in static class, and load it from there when scene changes.

Thanks for the advice, I’ll give that a shot!

@kafanasiff Did you fix it? Because I am facing the same issue and can’t resolve it.And i will be very grateful if you write the code here :wink:

I’m having a very similar problem too, did you manage to fix this?

When built, prefabs are read-only, meaning you can’t modify and save the main instance. After scene reloads it will load up the unchanged prefab. I’d recommend keeping the current sprite in the game manager and load it up on every scene (just as @Agent_007 proposed)

Hi @Jonas_Sid ,
Thank you for your reply.But can you give me a hint how to do that because i have no idea how?
I’ve searched alot but didn’t find the way.So i will be grateful if you do it :wink:

@abduomar , sorry for taking some time to reply.

You should try doing something similar to the game manager which is explained here: Level Generation - Unity Learn. Also, some more additions to the same game manager can be found here: Architecture and Polish - Unity Learn

Basically, in your instance of the game manager you should keep the current sprite variable you want the player object to have.
Create a method that would apply the sprite to prefab and use that method everytime you load a new level. In the provided example, InitGame() is used to load a new level, so add that sprite changing method in it and use your custom InitGame() method to load your levels.