I’m trying to create a virtual pet game with two different values to worry about, hunger and happiness. If anybody could tell me why hunger and happiness both start at 0 instead of 100 when I play the game it would be greatly appreciated.
I’m brand new to this so let me know if I need to post the complete script or not. Thanks.
My game manager script:
public class GameManager : MonoBehaviour {
public GameObject happinessText;
public GameObject hungerText;
public GameObject nameText;
public GameObject namePanel;
public GameObject nameInput;
public GameObject chao;
void Update ()
{
happinessText.GetComponent<Text>().text = "" + chao.GetComponent<Chao>().happiness;
hungerText.GetComponent<Text>().text = "" + chao.GetComponent<Chao>().hunger;
nameText.GetComponent<Text>().text = chao.GetComponent<Chao>().name;
}
My pet “chao” script:
public class Chao : MonoBehaviour {
[SerializeField]
private int _hunger;
[SerializeField]
private int _happiness;
void updateStatus()
{
if (!PlayerPrefs.HasKey("_hunger"))
{
_hunger = 100;
PlayerPrefs.SetInt("_hunger", _hunger);
}
else
{
_hunger = PlayerPrefs.GetInt("_hunger");
}
if (!PlayerPrefs.HasKey("_happiness"))
{
_happiness = 100;
PlayerPrefs.SetInt("_happiness", _happiness);
}
else
{
_happiness = PlayerPrefs.GetInt("_happiness");
}
public int hunger
{
get { return _hunger; }
set { _hunger = value; }
}
public int happiness
{
get { return _happiness; }
set { _happiness = value; }
}
