stringlapse
stringlapse

Reputation: 65

Unity 5.3 GUI Button Won't Call OnClick Event

So I'm trying to make a game with a title screen that you click to load the scene called Game. The button that I am using, created in the editor, doesn't work for whatever reason. When I create the button using code (Below), it does work. Why can't I use the editor to create my button and then call a function using OnClick by assigning it in the editor?

If I am going about making a 'click to play' title screen wrong please help me. Thanks in advance!

GUI.Button(new Rect(Screen.width * .25, Screen.height * .4, Screen.width * .5, Screen.height * .1), "Test")

Upvotes: 0

Views: 1564

Answers (2)

stringlapse
stringlapse

Reputation: 65

I solved the problem by making an empty GameObject and adding a box collider and a script. The script just calls an OnClick event like so:

function OnClick {
    //Code goes here
}

Hope this helped!

Upvotes: 0

S.Waraich
S.Waraich

Reputation: 9

try this

if (GUI.Button(new Rect(X, Y, width,height), "Button")) {
            Function();
        }

this will check if button is pressed and call function defined.

Upvotes: 1

Related Questions