11: Displaying clues [METHOD 1]

Step 1:
Create a UI text element in the canvas and position it where you want it to display.

Step 2:
We need to create a new script called messageScript and attach it to the box (All 5). This script will allow us to create a different clue in the inspector for each box.

JS

#pragma strict

@TextAreaAttribute(10,20)
var message : String;

C#

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

public class messageScript : MonoBehaviour {

    [TextArea(10,20)]
    public string message;
}

This code will create a text area so that you can write a clue for each box.
quest011 01

Step 3:
Now we need to access the message from the box we open and place it into the created UI text. Add the following elements to the interactionScript.

JS

#pragma strict

var B : GameObject;
private var COMP : float;

private var PROGRESS : float;
var bar : GameObject;
var messageDisplay : UI.Text;

function Start () 
{
    B.SetActive(false);
    COMP = 0;
    PROGRESS = COMP/100;
    bar.transform.localScale = new Vector3(PROGRESS,bar.transform.localScale.y,bar.transform.localScale.z);
 
messageDisplay.text = "A box is straight in front of you. It may hold a clue.";
}

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();
              COMP += 20;
              PROGRESS = COMP/100;
              bar.transform.localScale = new Vector3(PROGRESS,bar.transform.localScale.y,bar.transform.localScale.z);
             
 messageDisplay.text = hit.gameObject.GetComponent(messageScript).message;
               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;
float COMP;

float PROGRESS;
public GameObject bar;
public Text messageDisplay;

// Use this for initialization
void Start ()
{

     B.SetActive(false);
     COMP = 0;
     PROGRESS = COMP/100;
     bar.transform.localScale = new Vector3(PROGRESS,bar.transform.localScale.y,bar.transform.localScale.z);
   messageDisplay.text = "A box is straight in front of you. It may hold a clue.";
}


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();
              COMP += 20;
              PROGRESS = COMP/100;
              bar.transform.localScale = new Vector3(PROGRESS,bar.transform.localScale.y,bar.transform.localScale.z);
       
messageDisplay.text = hit.gameObject.GetComponent(messageScript).message;
               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);
     }

}

}

Step 4:
Drag the UI Text element into the inspector to attach it to the messageDisplay variable.

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.