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?
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
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.