How would I make a GUI popup (GUI texture) when I mouse over and click another in game object?
(I have books around my level and want a page from the book to popup when the player clicks the book)
I’m familiar with mouse over(s) and OnButtonDown functions to make animations run and instantiates happen, but I just can’t get my head around using these to control a GUI.
Not sure I understand what problem are you having. Designing your code, maybe?
I assume:
You want to only ever show one book page at a time (one must be closed to open another).
Mouse Over will pop up a small GUI (for instance with a “click to open” message).
Mouse Click will pop up a GUI with the book page, which won’t go until the user clicks ‘close’ or similar.
In that case, a way to do it would be to have in your books a script similar to yours:
(In pseudo-pseudocode):
bool displayClickMe;
BookGUI bookGUI; // Fetch a reference to the BookGUI instance on Start
function OnMouseOver()
{
displayClickMe = true;
}
function OnMouseExit()
{
displayClickMe = false;
}
function OnMouseDown()
{
// Open our book in the GUI.
bookGUI.open(this);
}
function OnGUI()
{
if(displayClickMe && !bookGUI.isOpen())
{
// Draw the small 'ClickMe' GUI.
GUI.DrawTexture.yourTexture;
}
}
Basically, you would attach a script such as that one to your books. When the books have the mouse over them they will be responsible of drawing a “click me” kind of GUI. When the books are clicked, they will be responsible of opening the book page GUI.
The book page GUI is a different script. Only one instance should exist, and not attached to books. It could be attached to the main camera, for instance. In each book page you would have to retrieve a reference to that instance, so that you can call the bookGUI.open() when you need to.
The BookGUI script itself should be something like (pseudo-code again):
function open(book)
{
// If we are already open we do nothing.
if(mIsOpen)
return;
mIsOpen = true;
mCurrentBook = book;
}
function isOpen()
{
return mIsOpen;
}
function OnGUI()
{
if(isOpen())
{
GUI.DrawTexture(...);
GUI.DrawLabel(mCurrentBook.name) // or whatever
if(GUI.Button("Close")) {
mIsOpen = false;
}
}
}
Basically, the BookGUI script would simply be responsible of drawing the GUI of a book whenever you want it to. When you bookGUI.open(book) you pass it the book you want to draw.
Does your game object have a collider attached? OnMouse only works if it does. If you already have a collider attached then i don’t see why its not working.