- Home /
How to make a game object light up only when its clicked on
Hi guys, so basically I got 4 game objects and i want them to light up only when they are clicked on. How do i do this please help.
Answer by Jessespike · Oct 26, 2016 at 05:38 PM
You can use OnMouseDown for clicks. Not sure what you mean by "light up"
void OnMouseDown() {
GetComponent<Renderer>().material.color = Color.green;
}
Ins$$anonymous$$d of GetComponent<Renderer>().material.color
, use GetComponent<Renderer>().material
and set that as 2 different materials with different tints.
Thank you for commenting @jessespike . What I mean by light up, for example if I have a cube as a game object and its green, I would want it to go to a bright green when I click on it then go back a to its normal color .
Use renderer.material
for this. $$anonymous$$ake 2 materials with different tints. Use them accordingly. (Set the two materials in your script using public $$anonymous$$aterial material1;
) and set your objects GetComponent().material
Answer by tiggy02 · Oct 28, 2016 at 05:47 PM
You need to create a light component then use OnMouseDown to detect a click then use that to change if the light is enabled or not.
private Light light;
void Awake() { light = GetComponent(); }
void OnMouseDown() { light.enabled = !light.enabled; }
Hi @tiggy02 I used your code but however keep getting an error when used. The error message is "Assets/Lightup.cs(19,1): error CS8025: Parsing error" I have copied and pasted the code down below can you please help.
using UnityEngine; using System.Collections;
public class Lightup : $$anonymous$$onoBehaviour {
private Light light;
void Awake() { light = GetComponent (); } void On$$anonymous$$ouseDown(){ light.enabled = !light.enabled; }
}
}
Hello @Yassassin07, sorry my code was never meant to be copy pasted. It was meant for you to look at then be able to learn, replicate, and change the code to conform to your needs. But if you really need a copy pas$$anonymous$$ble code here it is.
private Light light;
void Awake()
{
light = GetComponent<Light>();
}
void On$$anonymous$$ouseDown()
{
light.enabled = !light.enabled;
}
Your answer
Follow this Question
Related Questions
Ammount of Light received by a GameObject 1 Answer
How to detect if a GameObject is hit by a Spotlight 4 Answers
I need help with directional light 1 Answer
How to make a GameObject not hittable by shadows ? 2 Answers
Advanced Lighting 1 Answer