13: Turning on / off the boxes

Step 1:
Before we can control the active setting of the boxes we first need to create a control script which will contain the box game objects. For this script we will be writing our own function called ShowBox and using a switch statement. Create a script called boxControl and enter the script below. Attach the script to the player and drag the appropriate box assets into the five box slots.

JS

#pragma strict

var box1 : GameObject;
var box2 : GameObject;
var box3 : GameObject;
var box4 : GameObject;
var box5 : GameObject;

function Start()
{
     box1.SetActive(false);
     box2.SetActive(false);
     box3.SetActive(false);
     box4.SetActive(false);
     box5.SetActive(false);
}

function ShowBox (clueNum : int) {

switch(clueNum)
{
     case 1:
     box1.SetActive(true);
     break;

     case 2:
     box2.SetActive(true);
     break;

     case 3:
     box3.SetActive(true);
     break;

     case 4:
     box4.SetActive(true);
     break;

     case 5:
     box5.SetActive(true);
     break;
}

}

C#

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

public class boxControl : MonoBehaviour {

GameObject box1;
GameObject box2;
GameObject box3;
GameObject box4;
GameObject box5;

// Use this for initialization
void Start () {

     box1.SetActive(false);
     box2.SetActive(false);
     box3.SetActive(false);
     box4.SetActive(false);
     box5.SetActive(false);

}

// Update is called once per frame
void ShowBox (int clueNum) {
switch(clueNum)
{
     case 1:
     box1.SetActive(true);
     break;

     case 2:
     box2.SetActive(true);
     break;

     case 3:
     box3.SetActive(true);
     break;

     case 4:
     box4.SetActive(true);
     break;

     case 5:
     box5.SetActive(true);
     break;
}

}
}


Step 2:
Now we need to call the function created in the boxControl script from our interationScript. We do this by using the GetComponent command. We first set up a variable to record which clue number we are on, then we send this number to the boxControl script each time we open a box.

JS

#pragma strict

var B : GameObject;
private var COMP : float;

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

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.";
    collectedClues = 1;
    gameObject.GetComponent(boxControl).ShowBox(collectedClues);


}

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;
              collectedClues += 1;
              gameObject.GetComponent(boxControl).ShowBox(collectedClues);
               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;
int collectedClues;

// 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.";
    collectedClues = 1;
    gameObject.GetComponent<boxControl>().ShowBox(collectedClues);


}


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;
              collectedClues += 1;
              gameObject.GetComponent<boxControl>().ShowBox(collectedClues);
               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.