- Home /
[Newbie] Simple Menu script c#
Hi! I'm a newbie in Unity and I want to make a simple menu script,i have watched/read some tutorials, but i don't understand what the problem is. Here is the code:
using UnityEngine; using System.Collections;
public class Menu_Exit : MonoBehaviour
{
void OnMouseEnter()
{
renderer.material.color = Color.green;
}
void OnMouseExit()
{
renderer.material.color = Color.black;
}
void OnMouseDown()
{
Application.Quit();
}
}
When i start the application, nothing works. No color change, the application doesn't closes and I dont know why. Please help me Thanks!
Answer by robertbu · Feb 16, 2014 at 07:39 AM
You can only change the color of an object if the shader supports it. Create a material with the standard 'Diffuse' shader (which than Default-Diffuse), and coloring changes will work. You may want to throw a directional light in the scene.
As for Application.Quit(), from the reference:
Quit is ignored in the editor or the web player.
You can put a Debug.Log() inside OnMouseDown() to verify it is getting called. Note the object you have this script attached to must have a collider before any of the three callback will work.
I tried creating a material with the standard 'Diffuse' shader but the whole text become all black shape that I cannot see the text. Have I done something wrong?
I'm guessing at your problem, but a diffuse shader needs a light. Throw a directional light into the scene:
GameObject > Create Other > Directional Light
Note there are shaders that have a color that don't require lights. The 'Diffuse' was the first one I thought of that had a color parameter so you could test your code.
the light didn't work out. Anyway, the script work just fine with the "Cube" object, however they dont work on the "3D text", what does it means?
Ahhh, you are using a Text$$anonymous$$esh/3D Text object. You want to use the color property of the Text$$anonymous$$esh, not modify the material. So create a text mesh from scratch and back out of my material creation advice I game above. Assu$$anonymous$$g your script is on the same game object as the 3D Text, you can set the color by:
gameObject.GetComponent<Text$$anonymous$$esh>().color = Color.black;
Answer by unimechanic · Feb 21, 2014 at 11:30 PM
[Question solved, adding this answer to remove it from the Unanswered list.]
Your answer
Follow this Question
Related Questions
Problem with Pause and Pause Menu Script 1 Answer
Simple Menu Script 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to emit a sound on specific collision. 1 Answer
Need a script 0 Answers