OnMouseDown and GUI

I would like a GUI slider to appear and stay when the user clicks on an individual game object. Then I would like the user to be able to slide the slider and the g.o.'s alpha value changes. In doing so, this is what I have so far:

var slider : float=1;
var mouseToggle : boolean;

function OnMouseDown (){
	mouseToggle = !mouseToggle;
}

function OnGUI () {
	if (mouseToggle){
	slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 0.0, 1.0);
	renderer.material.color.a = slider;
	}
}

However, the slider does not appear when I click on the appropriate g.o. I posted in UnityAnswers already (see here) and subsequently tried different Input keys, but all unsuccessfully. :frowning:

There are several different g.o.s, each of which will have its own slider (among other GUI elements). I would like to activate each individual game object’s gui when OnMouseDown clicks on a new g.o.

Let me know if this makes sense.

Does your GameObject have a Collider on it with IsTrigger checked?

nope. should it?

Whether the collider is a trigger isn’t relevant.

–Eric

It just needs to be a collider, I’m not sure what’s wrong with this script

You guys rock. It worked as soon as I put the collider on it; thanks.

I kept reading that part in the scripting reference for OnMouseDown, but b/c I haven’t been using physics at all, I totally overlooked it. Just out of curiosity, why does OnMouseDown only work w/a collider or a GUIElement, rather than a straight up g.o.?

I don’t know why they did that, probably to give you more control over what you click rather then just it being a mesh renderer