- Home /
Prefab not working
i'm making 2D mobile game which is almost done but the only thing that prevents me from publishing it that prefab is not changing the image when i run the build on android device but in the Unity editor everything works like it should. Maybe someone could explain me what's the cause of this ?
Here is the code:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class playerchange1 : MonoBehaviour {
public Sprite img1;
public GameObject player;
private DimondManager DM;
private collectDimonds CD;
public bool isBought = false;
public GameObject Button;
void SetSprite()
{
player.GetComponent<Image> ().sprite = img1;
}
public void onClick ()
{
if (dimantuManager.dimonds >= 20 && isBought == false) {
SetSprite();
Button.GetComponent<Image> ().sprite = img1;
PlayerPrefs.SetInt ("Dimanti", + dimantuManager.dimonds - 20);
isBought = true;
Application.LoadLevel (Random.Range (0, 0));
}
else if (isBought == true) {
SetSprite();
Application.LoadLevel (Random.Range (0, 0));
}
}
}
The public gameobject player - is the prefab that is not changing the image when player clicks on the button which is also happening to the button prefab
Answer by Acid_kenobi · Apr 09, 2016 at 06:26 AM
Is there any code missing? Need to grab the references to your DimondManager and collectDimonds scripts ie:
void Start(){
DM = GetComponent<DimondManager>();
CD = GetComponent<collectDimonds>(); //Not sure why this is needed??
}
It also looks like dimantuManager is a different version of some kind of DimondManager?
public Sprite img1;
public GameObject player;
private DimondManager DM;
private collectDimonds CD;
public bool isBought = false;
public GameObject Button;
sprite playerSprite;
sprite buttonSprite;
void Start(){
DM = GetComponent<DimondManager>();
CD = GetComponent<collectDimonds>(); //Not sure why this is needed??
playerSprite = player.GetComponent<Image>().sprite;
buttonSprite = Button.GetComponent<Image>().sprite;
}
void SetSprite(){
playerSprite = img1;
}
public void onClick(){
if (dimantuManager.dimonds >= 20 && isBought == false){ //Where does dimantuManager come from?
SetSprite();
buttonSprite = img1;
PlayerPrefs.SetInt ("Dimanti", + dimantuManager.dimonds - 20);
isBought = true;
Application.LoadLevel (Random.Range (0, 0));
}
else if (isBought == true) {
SetSprite();
Application.LoadLevel (Random.Range (0, 0));
}
}
Your answer
Follow this Question
Related Questions
Whats the best way to populate a Dictionary with multiple Prefabs? 0 Answers
Unity not Instantiating Prefabs Properly 0 Answers
how do I assign a prefab's child to another prefab's script's public variable? 1 Answer
Ways to Avoid conflict between prefabs in source control 2 Answers
is having a Prefab reference inside a a Scriptable Object legit? 0 Answers