7: Adding UI Feedback

 Step 1:
We first need to create a canvas and UI text object for our message. Go To GameObject > UI > Canvas then add a text element by going to GameObject > UI > Text.
quest022

Step 2:
Now select the text element in the Hierarchy window and in the inspector window change the message to Press B to Interact. Also change the Font Size to 18 and Font Style to Bold. We also need to change the alignment to middle centre. Below these setting change the colour to white.
quest023
Your final UI text should look like this in game view.
quest024

Step 3:
Now we need to edit the interactionControls script by adding some new functions. we need the message to appear when we enter the trigger and dissapear when we exit the trigger. the variable B is the text object you created in the canvas, attach this element by dragging it to the slot in the inspector.

JS

#pragma strict

var B : GameObject;

function Start () 
{
    B.SetActive(false);
}

function OnTriggerStay(hit : Collider)
{
     if(hit.gameObject.tag == "BOX")
     {
           if(Input.GetButtonDown("Interact"))
          {
               var ani : Animator = hit.gameObject.GetComponent(Animator);
               ani.SetBool("OPEN", true);
               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;

public class interactionControls : MonoBehaviour {

public GameObject B;

// Use this for initialization
void Start ()
{

     B.SetActive(false);
}


void OnTriggerStay(Collider hit)
{

     if(hit.gameObject.tag == "BOX")
     {
          if(Input.GetButtonDown("Interact"))
          {
               Animator ani = hit.gameObject.GetComponent<Animator>();
               ani.SetBool("OPEN", true);
               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);
     }

}

}

 

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.