I’m working on a jetpack-type side-scrolling game to learn the basics of Unity and scripting. I’m currently stuck trying to get the ‘score’ to work.
I have two types of coins that I want the player to be able to collect as they play. One more common coin named ‘coin’, and another, less-common coin named ‘megaCoin’. I can successfully get the score to count up by one each time ‘coin’ is triggered, but cannot manage to get the score to go up by a further 10 when ‘megaCoin’ is triggered. Each GameObject (prefab) has its own tag; Coin and MegaCoin.
My code for the counting of the ‘coin’ is as follows:
public GUIText coinCount;
private float coins;
public void Update()
{
coinCount.text = coins.ToString("f0");
}
public void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Coin")
{
print("Coin collected!");
Destroy(coll.gameObject);
coins = coins + 1f;
}
}
Now with my ‘megaCoin’, I’m trying to link the SAME GUIText variable as in the ‘coin’ code, so I can get both scripts to contribute to the same number over time.
This is my ‘megaCoin’ script:
public GUIText coinCount;
private float coins;
public void Update()
{
coinCount.text = coins.ToString("f0");
}
public void OnTriggerEnter(Collider megaColl)
{
if (megaColl.gameObject.tag == "MegaCoin")
{
print("MEGACOIN Collected!");
Destroy(megaColl.gameObject);
coins = coins + 10f;
}
}
The problem is, as stated above, the ‘coin’ script works with increasing the number in the GUIText, however the ‘megaCoin’ script DOES NOT increase the number - triggering a ‘megaCoin’ does everything except increase the count (successfully prints, destroys etc.).
Where have I gone wrong?
Thanks!!