12: Displaying clues [METHOD 2]

The advantage of creating images of clues is that you can layout your clues to look however you wish. This could be in code form as in the example.

Step 1:
Create a PNG image which contains your clue. Similar to the example below. Import it into your project. make a note of the size your image has been created and make sure you create all other clues at the same size.
quest012 01

Step 2:
Select the imported PNG images in the project window and change the texture type to Sprite in the inspector.
quest012 02

Step 3:
Create a UI Image element in the canvas gameObject > UI > Image. Set the width and height to the same as the sprite. In this case 300 x 300.

quest012 03

Step 4:
Select the UI image and add the first clue sprite in the sourec image slot.
quest012 04
And should look something like this.
quest012 05

Step 5:
Now we need to script the changes in a very similar way to the prvious tutorial section. We will create a script to hold the image which is attached to each box, and then call that image from the main script. Create a new script called IMG_MessageScript and add the following lines. Drag the corresponding sprite images onto the IMGclue variable in the inspector.

JS

#pragma strict

var IMGclue : Sprite;

C#

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

public class IMGmessageScript : MonoBehaviour {

    public Sprite IMGclue;
}

Step 5:
Now we will amend the intecactionScript with the following amedments.

JS

#pragma strict

var B : GameObject;
private var COMP : float;

private var PROGRESS : float;
var bar : GameObject;
var imageDisplay : UI.Image;

function Start () 
{
    B.SetActive(false);
    COMP = 0;
    PROGRESS = COMP/100;
    bar.transform.localScale = new Vector3(PROGRESS,bar.transform.localScale.y,bar.transform.localScale.z);
 

}

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);
             
imageDisplay.sprite = hit.gameObject.GetComponent(IMG_MessageScript).IMGclue;
               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 Image imageDisplay;

// 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);
}


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);
     
 imageDisplay.sprite = hit.gameObject.GetComponent(IMG_MessageScript).IMGclue;
               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.