Hi,
I’m new at Unity and i’m trying to develop a script that everytime that I click on a cube, this cube will “light”.
I attach the code i wrote because i don’t know where i’m failing.
#pragma strict
var cube : GameObject;
Event e : Event.current;
function Start () {
cube.light.enabled = false;
}
function Update () {
if(e.type = EventType.MouseUp){
cube.light.enabled = true;
}
This code is attached to my cube object in my project.
Excuse me if i’m too newbie…
Thanks
Rather than use the event type, I would suggest using the built in events in MonoBehaviour. Then tie it down with the Update (or so)
var cube : GameObject;
var lightState : boolean = false;
function OnMouseDown(){
lightState = true;
}
function Update(){
cube.light.enabled = lightState;
}
This is all well and wonderful, but what happens when you want to use the cube as a light switch?
You simply change how you set the state.
var cube : GameObject;
var lightState : boolean = false;
function OnMouseDown(){
lightState = !lightState ;
}
function Update(){
cube.light.enabled = lightState;
}
Yay, now, lets look at a timed light. So you click on the cube, the light turns on for a few seconds then turns its self off. To do this, you use something called a Coroutine.
var cube : GameObject;
var lightState : boolean = false;
function OnMouseDown(){
StartCoroutine(DoLight(3));
}
function DoLight( duration : float){
cube.light.enabled = true;
yield WaitForSeconds ( duration );
cube.light.enabled = false;
}
This is actually a decent example of a finite state. (where a single variable controls the actions of an object)