x


Making a Button in unity?

im creating a very simple scene in unity where u pick up a cube and place it on a button to make a bridge appear. i know almost nothing about scripting and am still very new to all of this so any information is helpful. i need the bridge to only activate when the cube has been placed on top of the button. and when the cube has been removed, the bridge needs to disappear. i can make the button activate when anything touches it with the activate trigger script but i want it to only activate on and off when the cube is touching it... any help would be great

more ▼

asked Jun 24 '12 at 05:55 PM

Quadraxis86 gravatar image

Quadraxis86
0 1 2 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You will want to specify the object by either using a tag comparison or name comparison. Below is an example using tag(since if you clone the cube its name will go from "Cube" to "Cube(Clone)" thus preventing the code from continuing..

function OnTriggerEnter(other:Collider)
{
    if(other.tag=="BridgeCube")
    {
        myBridge.collider.enabled=true;//turn on collider
        myBridge.renderer.enabled=true;//turn on renderer
    }
}
function OnTriggerExit(other:Collider)
{
    if(other.tag=="BridgeCube")
    {
        myBridge.collider.enabled=false;//turn off collider
        myBridge.renderer.enabled=false;//turn off renderer
    }
}
more ▼

answered Jun 24 '12 at 06:38 PM

hijinxbassist gravatar image

hijinxbassist
2.1k 23 31 38

(comments are locked)
10|3000 characters needed characters left

Not 100% sure if it works... but I'm thinking it will. Adjust the timer to 0.1 if you want instant release, if not.... Poof. You've got a timer.

var Bridge : GameObject
var Timer : float = 1;

function Update(){

var TimerUser
//Timer Counts Down
TimerUser -= Time.Deltatime;


//Stops from going below 0
if(TimerUser < 0 )
TimerUser = 0;


//If there is time left, Bridge is active, else it is not active
if(TimerUser > 0)
Bridge.active = true;

else
Bridge.active = false;


}


function OnTriggerStay( Col: collider) {


if Col.GetTag("Box")
TimerUser = Timer;

}
more ▼

answered Jun 24 '12 at 09:47 PM

Dev Solo gravatar image

Dev Solo
0 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5268
x2582
x819
x297
x11

asked: Jun 24 '12 at 05:55 PM

Seen: 406 times

Last Updated: Jun 24 '12 at 09:47 PM