- Home /
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
Answer by hijinxbassist · Jun 24, 2012 at 06:38 PM
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
}
}
Answer by Dev Solo · Jun 24, 2012 at 09:47 PM
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;
}
Your answer
Follow this Question
Related Questions
Audio play when object hit collision. 2 Answers
script to create gui when detection collision between cube and first controler person ?? 1 Answer
How to make movement on camera on button down. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to check if an object is colliding with another from another script ? 1 Answer