- Home /
GUI Texture not responding to OnMouseEnter
I have a problem creating a simple pause menu in my game and this is my first time working with GUI. My problem is when I move over the text in the pause menu its not recognizing it, the text doesn't turn red and the buttons don't work. I am using two different scripts I have found on the web, combined and slightly modified them. Does it have anything to do with having to use the "Draw" function? Thanks for reading my question. On a side question for this script when I unpause the game using the Esc key the background music starts over, how do I fix that? Here is the script:
#pragma strict
var pause : boolean = false;
var resumeGUI : GUITexture;
private var allAudioSources : AudioSource[];
resumeGUI.enabled = false;
function Awake()
{
allAudioSources = FindObjectsOfType(AudioSource) as AudioSource[];
}
function PauseAllAudio()
{
for(var audioS : AudioSource in allAudioSources) { audioS.Pause(); }
}
function ResumeAllAudio()
{
for(var audioS : AudioSource in allAudioSources) { audioS.Play(); }
}
function Update()
{
if(Input.GetKeyUp(KeyCode.Escape))
{
if(pause==true)
{
pause = false;
}
else
{
pause = true;
}
if(pause == true)
{
Time.timeScale = 0.0;
resumeGUI.enabled = true;
PauseAllAudio();
}
else
{
Time.timeScale = 1.0;
resumeGUI.enabled = false;
ResumeAllAudio();
}
}
}
var isMainMenu=false;
function OnMouseEnter()
{
//change text color
guiTexture.color = Color.red;
}
function OnMouseExit()
{
//change text color
guiTexture.color = Color.white;
}
function OnMouseUp()
{
//is this quit
if (isMainMenu==true)
{
//load main Menu
Application.LoadLevel("AliensMainMenu");
}
else
{
Time.timeScale = 1.0;
resumeGUI.enabled = false;
ResumeAllAudio();
}
}
@robertbu: On$$anonymous$$ouse* functions work perfectly fine with GUIElements. They don't need colliders.
Thanks Eric. I was visualizing the GUI.* calls, but that's clearly not what he is using.
Answer by Chronos-L · Mar 04, 2013 at 01:57 AM
Can you post a screenshot? I am not sure how many gui element you have right now (i think there are 1 pause and 1 resume), and what gameObject you are attaching this script to? (empty, guiTexture, guiText or something else)
Answer to side question
Answered question in UnityAnswers : Continueing music after un-pausing
Edit (3rd Rev)
In Unity's documentation on OnMouseOver, your overwritten version of OnMouseOver()
is called every time your mouse is over the GUIElement or Collider this script is attached to. However, in your case, you attach it to the camera (which is neither GUIElement or Collider).
In your question, you said that you combined and modify 2 scripts, so I am quite sure that all the OnMouse
function are from 1 script. What you should do now is to make it 2 script again ( I throw in a few lines to allow communication between 2 scripts ).
Proposed Solution
Pause Script (Attach to camera or empty object)
#pragma strict
var pause : boolean = false;
var clickTexture : ClickTexture[];
private var allAudioSources : AudioSource[];
function Awake()
{
EnableClick( false );
for( var i : int = 0; i < clickTexture.Length; ++i )
clickTexture[i].pauseScript = this;
allAudioSources = FindObjectsOfType(AudioSource) as AudioSource[];
}
function PauseAllAudio()
{
for(var audioS : AudioSource in allAudioSources) { audioS.Pause(); }
}
function ResumeAllAudio()
{
//ResumeAllAudio is not fixed
for(var audioS : AudioSource in allAudioSources) { audioS.Play(); }
}
function Update()
{
if(Input.GetKeyUp(KeyCode.Escape))
{
//Super lazy one-liner
pause = !pause;
if(pause == true)
{
Time.timeScale = 0.0;
EnableClick( true );
PauseAllAudio();
}
else
{
Time.timeScale = 1.0;
EnableClick( false );
ResumeAllAudio();
}
}
}
function RunOption( str : String ) {
if( str == "quit" ) {
Application.LoadLevel("AliensMainMenu");
}
else if( str == "resume" ) {
Time.timeScale = 1.0;
EnableClick( false );
ResumeAllAudio();
}
}
function EnableClick( b : boolean ) {
for( var i : int = 0; i < clickTexture.Length; ++i )
clickTexture[i].Show(b);
}
ClickTexture Script (Attach to GUITextre/GUIText)
var pauseScript : Pause;
var optionString : String;
function OnMouseEnter()
{
//change text color
guiTexture.color = Color.red;
}
function OnMouseExit()
{
//change text color
guiTexture.color = Color.white;
}
function OnMouseUp()
{
pauseScript.RunOption( optionString );
}
function Show( b : boolean ) {
this.enabled = b;
if( guiTexture ) {
guiTexture.enabled = b;
}
if( guiText ) {
guiText.enabled = b;
}
}
Yeah I have two options in the pause menu, Resume and Quit To $$anonymous$$enu. I have attached it to the $$anonymous$$ain Camera which is labeled camera. and thank you for the answer to my other question. the two options are supposed to turn red when I hover over them with the mouse but its not detecting that.
Look at my answer again. I added in my answers for your main question.
Thanks for the reply but its showing errors for lines 11 and 13 in the first script and it wont let me do anything. i cant figure out what the errors are telling me because it looks right.
the 3 errors are :
Assets/Scripts/Pause$$anonymous$$enu.js(11,18): BCE0044: expecting ;, found 'i'.
Assets/Scripts/Pause$$anonymous$$enu.js(11,54): BCE0043: Unexpected token: ).
Assets/Scripts/Pause$$anonymous$$enu.js(13,42): BCE0044: expecting :, found '='.
Sorry, I was using C# syntax ( I start Unity with JS, then I switch to C# later, so I just type in C# unconsiously ). I compile the script in my Playground project, and I fix some of the syntax.
Look at my answer again.
I fixed the following:
Line 11 :
for( int i = 0; ...
, syntax errorLine 12 :
clickTexture.
, forgot to use indexLine 51 :
function RunOption( string str )
, syntax errorLine 62 :
function EnableClick( bool b )
, syntax errorLine 63 : Same error as line 11
Line 64 : Same mistake as line 12
Thank you I am soooo close but I have one error now:
Assets/Scripts/ClickTexture.js(3,19): BCE0018: The name 'Pause' does not denote a valid type ('not found').
Also I thought that might have been the problem with the different syntax but i didn't get around to trying to change the scripts. But thank for showing me the javascript version.