Basic OnMouseDown menu navigation with a GUI Texture

Hello All,

First post here, and I’m more of an artist… so please bear with me :slight_smile:

I have 2 SCENES in my project, which I have added in the BUILD SETTINGS, and all I’m trying to do is navigate from one scene to another.

The only thing I have in the FIRST scene ATM is the camera and a GUI Texture.

On this GUI Texture I’ve attached the following .JS

function OnMouseDown () {
Application.LoadLevel ("name_of_2nd_scene");
}

Should work right? Yet nothing happens when I click on the GUI Texture… :confused:

I’ve gone through all the tutorials I could get my hands on, and I just spent a couple of hours searching the forums regarding this seemingly simple task…

Am I forgetting something here?

Apologies if I’ve overlooked the answer in a previous post… and thanks in advance :slight_smile:

No that will not work as GUI Texture is no 3D object in the traditional sense.

You will have to use the update function and check the mouse position against the rect filled by the gui texture and if the position is in the rect when the mouse button is clicked, you would load the other scene

Thank you dreamora for the instant response!

Any quick pointers, links or examples as to how that can be achieved? :smile:

I guess I could try to hack it by adding an invisible 3d object behind the GUI Texture and applying the script to that instead~~?

Not as clean as the method suggested I’m sure…

OK… I just tried adding the same script to a 3D Plane instead of a GUI Texture and it STILL doesn’t seem to work… :confused:

You cant just attach that code to an object and have the object trigger on a click.

Make sure you have a colider on the object

OnMouseDown specifics:

Colider specifics:

the 3D plane I attached the script to already had a built-in colider…

Also, I’m realizing that that is a pretty messy way to go about it, especially when I try to move the “group” around :confused: …was worth a shot anyway…

Can somebody please provide me with an example of how I can use an update function to check the mouse position against the rectangle filled by a GUI Texture :?:

Much appreciated…

This is wrong. OnMouseDown works fine with plain GUITextures as-is. The docs say:

MonoBehaviour.OnMouseDown
function OnMouseDown () : void
Description
OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.

Not necessary. As noted above, what you were doing originally is fine. Do a simple test:

function OnMouseDown () {
   print ("Clicked");
}

If that works (which it should, works fine here), then something else is going wrong with your loadlevel code.

–Eric

Hello,
Might I hazard a guess that name_of_2nd_scene is a variable? If so, all you need to do is remove the quotation marks like so:

function OnMouseDown(){
 Application.LoadLevel(name_of_2nd_scene);
}

If it was just for the purposes of your question, then ignore me. :slight_smile:

Regards,

-Lincoln Green

EDIT: Also, make sure that your scenes are added in the ‘Build Settings’ window.
Note that both of the above problems would produce some kind of console message…

Thank you for your responses Eric5h5 and FlamingHairball

function OnMouseDown () {
   print ("Clicked");
}

Gives me no different results than…

function OnMouseDown () {
Application.LoadLevel ("name_of_2nd_scene");
}

…in the sense that Nothing happens at all…

Here is something that Does work however…

function Update () {
   if (Input.GetKeyDown("x"))
   Application.LoadLevel ("name_of_2nd_scene");
}

…which leads me to believe that there Is nothing wrong with my LoadLevel code…
Unfortunately, I want to be able to Click on the GUI Texture and not just press a random key… :confused:

Also, removing the quotation marks from the scene name simply produces an error, and as I’ve mentioned before, the scenes are indeed added in the “Build Settings” window.

I’ve tried All of this on 2 separate computers, thinking that maybe its a faulty Unity install, one running on Vista, the other on XP. Still, in either case it just doesn’t want to work for me.

So once again, could somebody please post a tried&true example of the OnMouseDown fuction?
…or does…

function OnMouseDown () {
Application.LoadLevel ("sceneName");
}

…just work for everyone else except me and my 2 computers?:frowning:

…It sounds like dreamora has the right idea when (he) said…

… I just have no idea of how to go about that.

I already did. :wink: Scanning rects in Update is pointless when OnMouseDown works fine. Attached is an example project; if you click on the Unity logo, it disappears. (If you’re on Windows, unzip using an unarchiver that works, not the built-in one.)

–Eric

233853–8363–$onmousedownproject_577.zip (1.06 MB)

Thanks for the attachment Eric5h5!
I got some good news, and some bad news… :confused:

It seemed like you’ve been testing this on a Mac so…I borrowed my roommate’s MacBook Pro, tried it there, and it turns out that everything we’ve been talking about Does work after all…

function OnMouseDown () {
Application.LoadLevel ("sceneName");
}

…as well as what you included in OnMouseDownProject

function OnMouseDown () {
	Destroy(gameObject);
}

…it just DOESN’T WORK in a WINDOWS environment… :confused:

It seems that this might be a bug with the 2.6 version (for windows only)~~?

Can anyone verify this on a Windows machine?

I hope I’m not going to have to buy a MacBook now…:confused:

Perhaps a fix in a (near)future Unity release?

Maybe 2.6.1? That future is so near, it happened days ago…

Not at all. You can buy a Mac mini, an iMac, or a Mac Pro. :wink:

–Eric

Looks like I can hold off on buying a Mac, for now!

The 2.6.1 update solved the Problem!!!

Thank you Eric5h5! :smile:

gotta remember to stay on top of those updates :sweat_smile:

Eh, buy the Mac anyway.

Onmouse down doesn’t work for me. I tried erics sample project but get no love.

There isn’t any kind of position I need to put the GUItexture, right? It should always stay on top? I’m stumped.

I have a Mac mini and Unity Basic btw.