- Home /
GUI Button not working (Unexpected Symbol)
Here's my script, for some reason it says there's an expected symbol & parsing error.
But if I take away the GUI Button line on line twelve it works fine.
I just want a GUI Texture to show up when I click on a mesh
Here's my script: using UnityEngine; using System.Collections;
public class OnClick : MonoBehaviour {
public Texture2D texture = null;
public float x = 0;
public float y = 0;
public float x2 = 0;
public float y2 = 0;
void OnMouseDown (){
if (Input.GetKey ("mouse 0")) {
GUI.Button(new Rect(x,y, x2,y2), texture)
}
}
}
Now there's no errors, but when I click on the mesh nothing shows up after assigning a texture :/
Now there's no errors, but when I click on the mesh nothing shows up after assigning a texture :/
Answer by PvTGreg · Sep 02, 2014 at 02:01 PM
you need to put the Gui inside the OnGUI function
Hi ok try this at the moment put this outside of the onmouse down for future all gui has to be done inside the OnGUI function
void OnGUI ()
{
GUI.Button(new Rect(x,y, x2,y2), texture)
}
i dont realy know what your trying to do . are you trying to change the texture of the button when you press it?
Answer by kacyesp · Sep 02, 2014 at 02:06 PM
public class OnClick : MonoBehaviour {
public Texture2D texture = null;
public float x = 0;
public float y = 0;
public float x2 = 0;
public float y2 = 0;
void OnGUI() {
if ( ! texture ) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if ( GUI.Button(new Rect(x,y, x2,y2), texture) )
Debug.Log( "Do Something" );
}
}
Here's a link to the documentation for GUI.Button if you need it: http://docs.unity3d.com/ScriptReference/GUI.Button.html
Answer by aldonaletto · Sep 02, 2014 at 02:06 PM
GUI items only can appear inside the OnGUI function (or some function called by OnGUI). You could in this case set a variable that enables the GUI item - which should be a GUI.Label or GUI.DrawTexture instead of GUI.Button (a button responds to the mouse click, what may cause weird collateral effects). For instance:
...
private bool enableTexture;
void OnMouseDown (){
enableTexture = true; // enable GUI when mouse pressed over object
}
void OnMouseUp (){
enableTexture = false; // disable GUI when mouse button released
}
void OnGUI (){
if (enableTexture){
GUI.Label(new Rect(x,y, x2,y2), texture);
}
}
And remember to end the instructions with a semicolon, like @AyAMrau pointed out.
Thank you so much, i've been for the past hour for a script like this.
This works perfectly, and now that I know how to set variables in C# like that, there's no stopping me!
also, how am I supposed to add buttons to click on when i click on a mesh then, if I can't add a GUI Button?
Your answer
Follow this Question
Related Questions
Parsing Error : Unexpected symbol 'end of file' 2 Answers
changing GUI Button text with a string array 2 Answers
Problem with multi-touch on android game 1 Answer
Add EditorUI elements when a button is pressed 0 Answers
Bullet Prefab not instantiating 1 Answer