Question by
Anonymou5 · Sep 27, 2016 at 02:41 PM ·
gameobjectbuttonlighttogglemouseclick
Activate when mouse klick object
I'm trying to make button that activates and deactivates light.
I have tried following script but it doesn't seem to work.
using UnityEngine;
using System.Collections;
public class ToggleLightButton : MonoBehaviour
{
public Light Light;
void OnMouseDown ()
{
Light.enabled = true;
}
}
Comment
Answer by OctoMan · Sep 27, 2016 at 02:48 PM
create a public function
public void LightOn()
{
Light.enabled = true;
}
Now drag the script on the light or anywhere. Then drag the gameobject in the buttons onClick event, and choose the function.
Should work.
If its a 3d object you need to use raycast i believe.
bool on = false;
void Update()
{
if ( Input.Get$$anonymous$$ouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if(hit.transform.tag == "LightSwitch")
{
on = !on;
Light.enabled = on;
}
}
}
}