9: Adding percentage completion

Step 1:
In this game we have five boxs placed around the game map. To do this we first need to create a prefab object from the box we have created. Right click in the project window and select Create > Prefab. Name the prefab box and drag the box we have creaed in the hierarchy window onto the prefab in the project window. It should change from a gery cube to a thumbnail image of the box. If you select the newly created prefab it will have all the components we have attached to it visible in the inspector.

Step 2:
Place 5 box prefabs around your game level, Simply drag them from the project window. Check that you can open all of them before moving on.

Step 3:
Add a UI Image to your canvas and set the width and height to look like a full progress bar. You can also set the colour to what ever you wish.
quest026
We also need to look at some of the other settings. Set the x pivot to 0 and the Anchor point to middle left by selecting the red crosshair icon above the word Anchors. Position the new progress bar where you want it on your canvas. Now we can script it.

Step 4:
WE now need to add some extra code to our interactionControls script. We need to create two variables, one to hold our current progression and one to hold the progress bar game object we just created. We will then calculate the precentage completed and scale the progress bar accordingly. You could also write the new code in its own function if you wish. Attach the progress bar image to the bar variable in the inspector. The bar will now move up 20% each time you open a box.

JS

#pragma strict

var B : GameObject;
private var COMP : float;
private var PROGRESS : float;
var bar : GameObject;

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

// 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);
               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 final bar should look something like this...
quest00903

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.