Search Unity

Add Number to GUI when object is collected

Discussion in 'Scripting' started by JackFranklin96, Jan 28, 2015.

  1. JackFranklin96

    JackFranklin96

    Joined:
    Jan 21, 2015
    Posts:
    16
    Hi

    I'm trying to make it so that when I collect an object it adds a one to the counter that I've added using a Game GUI script but I'm not too sure how to do it.

    I've tried using this code but it keeps coming up with errors when I try to play my scene:

    using UnityEngine;
    using System.Collections;

    public class GameGUI : MonoBehaviour {

    public int counter = 0;
    public int timeLeft = 30;
    public GUIStyle mystyle;

    void OnGUI () {
    GUI.Label(new Rect(50,Screen.height-45,100,30), "Collected: "+counter, mystyle);

    }
    }
    void OnTriggerEnter2D (Collider2D obj) {
    if(obj.gameObject.tag=="Player"){
    +=(1) counter, mystyle}

    I've also got code on the objects that I'm collecting so that they destroy themselves when my game character enters their box collider.

    Thanks
     
  2. Ryaann

    Ryaann

    Joined:
    Jan 28, 2015
    Posts:
    4
    I think it should be:

    Code (CSharp):
    1. void OnTriggerEnter2D (Collider2D obj) {
    2. if(obj.gameObject.tag=="ThingYou'reCollecting"){
    3. counter += 1;
    4. }
    Could even destroy the object in there too:

    Code (CSharp):
    1. void OnTriggerEnter2D (Collider2D obj) {
    2. if(obj.gameObject.tag=="ThingYou'reCollecting"){
    3. counter += 1;
    4. Destroy(obj);
    5. }
     
  3. JackFranklin96

    JackFranklin96

    Joined:
    Jan 21, 2015
    Posts:
    16
    Hi

    Thanks for the help I got it working