Multiple triggers updating one GUIText

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!!

You don’t need two separate scripts for this. In fact, one script is much easier!

One solution is to have the player hold one coin script. When the player collides with a coin, the OnCollisionEnter event will fire. Check the tag of the object (coin or megaCoin) to decide the value of the coin.

I’ve coded a simple script to accomplish this:

using UnityEngine;
using System.Collections;

public class PlayerCoins : MonoBehavior {

    public float coinScore;
    public GUIText scoreKeeper;

    void Update()
    {
        scoreKeeper.text = "" + coinScore;
    } 

    void OnCollisionEnter(Collision c)
    {
        if(c.transform.tag == "Coin")
        {
            coinScore++;
            Destroy(c.transform.gameObject);
        }
        else if(c.transform.tag == "MegaCoin")
        {
            coinScore += 10;
            Destroy(c.transform.gameObject);
        }
    }
}

Remove the other two scripts and apply this script to the player.

If you’re using triggers, just use OnTriggerEnter(Collider c) instead of OnCollisionEnter(Collision c).

Keeping track of one score variable is MUCH easier than having two separate ones, and the overall complexity of the scripting is also lower.

First, you dont’t initialization your variable “coins”.Second, create one script base on your two. Example see below:

 public GUIText coinCount;
 private float coins;

 void Start() {
  coins =0;
 }

 void Update() {
  coinCount.text = coins.ToString("f0"); 
 }

 void OnTriggerEnter(Collider coll) {
  if (coll.gameObject.tag == "Coin") {
   print("Coin collected!");
   Destroy(coll.gameObject);
   coins = coins + 1f;
  } else if (coll.gameObject.tag == "MegaCoin") {
   print("MEGACOIN Collected!");
   Destroy(coll.gameObject);
   coins = coins + 10f;
  }
 }

And don’t forget attach this script to any object your scene, for example, MainCamera. I hope that it will help you.