- Home /
Changing a prefab's sprite doesnt work on mobile, but it does on editor, how s it possibble ?
Hi,
I have two scenes. In the first one, the Play scene, i have the Character Prefab, which contains a particular sprite. The second one, the Shop Scene, contains 12 sprites which should change the sprite of the Character prefab on touch. However, even if in the editor, the change happens, in Android it doesnt.
Before pointing out that it's because of OnMouse Down, i want to say that if i take the sprite from my Character prefab to put in the shop, it does, but not the other way around,
The code is as follows:
public class CharacterSprite : MonoBehaviour { [SerializeField] public GameObject Character; private Transform face;
void OnMouseDown () { // This (1) works because i tested the code at (2)
SpriteRenderer charSprite = Character.GetComponentInChildren<SpriteRenderer>();
SpriteRenderer unlockSprite = this.gameObject.GetComponent<SpriteRenderer>();
Debug.Log (charSprite.sprite);
charSprite.sprite = unlockSprite.sprite;
//unlockSprite.sprite = charSprite.sprite; -> (2) This code works, i tested it
// faceT = Character.GetComponentInChildren<Transform>();
Debug.Log (charSprite.sprite);
Transform faceT = Character.transform.GetChild(0);
if (charSprite.sprite.name == "1300" ||charSprite.sprite.name == "s1a" ||charSprite.sprite.name == "s2b")
{
Debug.Log (faceT.localScale);
faceT.localScale = new Vector3 (0.47f,0.47f,1.0f);
}
if (charSprite.sprite.name == "ss2" ||charSprite.sprite.name == "s4a" ||charSprite.sprite.name == "s4b")
{
Debug.Log (faceT.localScale);
faceT.localScale = new Vector3 (0.55f,0.55f,1.0f);
}
if (charSprite.sprite.name == "s2a" ||charSprite.sprite.name == "s2b" ||charSprite.sprite.name == "ss1")
{
Debug.Log (faceT.localScale);
faceT.localScale = new Vector3 (0.485f,0.485f,1.0f);
}
if (charSprite.sprite.name == "s3b" ||charSprite.sprite.name == "s3a" ||charSprite.sprite.name == "ss3")
{
Debug.Log (faceT.localScale);
faceT.localScale = new Vector3 (0.47f,0.47f,1.0f);
}
}
}
Your answer
Follow this Question
Related Questions
How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers
Is there a way to change the sprite for all instances of the same object simultaneously? 1 Answer
Prevent long scene loading times due to prefabs in Android? 1 Answer
Put all Assets in Resources Folder ? 1 Answer
Instantiating Prefabs Lags in Android 0 Answers