- Home /
Problem whit score system?
Hi, im making a Train of though clone (a luminosity.apk game from the playstore). In my score system, the player gain a point everytime a train collide with a house of the same color. I made every train and house an independnt GameObject, and I atached this piece of code to every house.
public int puntos;
// Use this for initialization
void Start ()
{
puntos = 0;
}
void Update ()
{
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.name == "TrenRojo" && this.gameObject.name == "CasaRoja")
{
other.gameObject.SetActive (false);
IncrementarPuntos ();
}
else
{
other.gameObject.SetActive (false);
}
if (other.gameObject.name == "TrenRojoO" && this.gameObject.name == "CasaRojaO")
{
other.gameObject.SetActive (false);
IncrementarPuntos ();
}
else
{
other.gameObject.SetActive (false);
}
if (other.gameObject.name == "TrenAzul" && this.gameObject.name == "CasaAzul")
{
other.gameObject.SetActive (false);
IncrementarPuntos ();
}
else
{
other.gameObject.SetActive (false);
}
if (other.gameObject.name == "TrenVerde" && this.gameObject.name == "CasaVerde")
{
other.gameObject.SetActive (false);
IncrementarPuntos ();
}
else
{
other.gameObject.SetActive (false);
}
if (other.gameObject.name == "TrenVerdeO" && this.gameObject.name == "CasaVerdeO")
{
other.gameObject.SetActive (false);
IncrementarPuntos ();
}
else
{
other.gameObject.SetActive (false);
}
if (other.gameObject.name == "TrenNaranja" && this.gameObject.name == "CasaNaranja")
{
other.gameObject.SetActive (false);
IncrementarPuntos ();
}
else
{
other.gameObject.SetActive (false);
}
if (other.gameObject.name == "TrenMorado" && this.gameObject.name == "CasaMorada")
{
other.gameObject.SetActive (false);
IncrementarPuntos ();
}
else
{
other.gameObject.SetActive (false);
}
}
void IncrementarPuntos ()
{
puntos = puntos + 1;
Debug.Log (puntos);
}
The problem is that every time a new train collide whit a new house, the score return to 0, so the score its always 1. Sorry for my english, and also for my code, im new in unity.
Thanks :)
Answer by DoTA_KAMIKADzE · May 05, 2015 at 05:08 AM
You need to have a single instance of your "puntos" variable. You can do that in few ways, choose one of them:
1) Just make it static:
public static int puntos;
2) Create a custom script that will hold your "puntos" variable and get the instance reference of that script in each script where you'll need it and increment variable of that custom script.
3) Save/load your score from/to some file (e.g.: .txt) or use something like PlayerPrefs.
P.S. Personally I'd go for the first one.
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad Game Score/Lives 1 Answer
Having trouble updating the text in my GUI 2 Answers
How to get screen coordinate of plane 1 Answer
Stop Score From Adding? 1 Answer
I need help for delete old highscore 0 Answers