10: Alternate completion feedback

Instead of using a progress bar you can have a number of clues found UI element. In this section we will look at setting this up. It will show how many clues have been found and how many remain.

Step 1:
Add a UI text element to your canvas and position it where you require.

Step 2:
Amend the interactionControls script with the following: (REMEMBER this script is added instaed of the script in the previous section) . Add the newly created UI text element to the completionText variable in the inspector.

JS

#pragma strict

var B : GameObject;
private var COLLECTED : int;
private var NEEDED : int;
var completionText : UI.Text;

function Start () 
{
    B.SetActive(false);
    COLLECTED = 0;
    NEEDED = 5;
    completionText.text = COLLECTED + " / " + NEEDED;
}

function OnTriggerStay(hit : Collider)
{
     if(hit.gameObject.tag == "BOX")
     {
           if(Input.GetButtonDown("Interact"))
          {
               var ani : Animator = hit.gameObject.GetComponent(Animator);
               ani.SetBool("OPEN", true);
               var AS : AudioSource = hit.gameObject.GetComponent(AudioSource);
               AS.Play();
              COLLECTED += 1;
              completionText.text = COLLECTED + " / " + NEEDED;
               Destroy(hit.gameObject.GetComponent(Collider));
               B.SetActive(false);
          }
     }
}

function OnTriggerEnter(hit : Collider)
{
    if(hit.gameObject.tag == "BOX")
    {
        B.SetActive(true);
    }

}


function OnTriggerExit(hit : Collider)
{
    if(hit.gameObject.tag == "BOX")
    {
        B.SetActive(false);
    }
}

C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class interactionControls : MonoBehaviour {

public GameObject B;
int COLLECTED;
int NEEDED;
public Text completionText;

// Use this for initialization
void Start ()
{

     B.SetActive(false);
     COLLECTED = 0;
     NEEDED = 5;
     completionText.text = COLLECTED + " / " + NEEDED;
}


void OnTriggerStay(Collider hit)
{

     if(hit.gameObject.tag == "BOX")
     {
          if(Input.GetButtonDown("Interact"))
          {
               Animator ani = hit.gameObject.GetComponent<Animator>();
               ani.SetBool("OPEN", true);
               AudioSource AS  = hit.gameObject.GetComponent<AudioSource>();
               AS.Play();
              COLLECTED += 1;
              completionText.text = COLLECTED + " / " + NEEDED;
               Destroy(hit.gameObject.GetComponent<Collider>());
               B.SetActive(false);

          }
     }
}

void OnTriggerEnter(Collider hit)
{
     if(hit.gameObject.tag == "BOX")
     {
          B.SetActive(true);
     }
}

void OnTriggerExit(Collider hit)
{
     if(hit.gameObject.tag == "BOX")
     {
          B.SetActive(false);
     }

}

}

The image below shows both completion feedback methods working...
quest010 01

Casino Bet 365 is best casino in the world.

What's Next

Next up for Unity Snippets is a look at game development articles from across the web...

Free Templates - bigtheme.net
Popular Art Betting make bookies articles.