- Home /
Click on a gameObject to show/hide a GUI box help C#
i need help modifying two scripts
i have a lot of gameObjects, if you click in each one, it will show a text from a GUI in the mainCamera of a especific gameObject.
need* another text and a GUI box for each model you touch with the mouse button 1 on another or the same scripts
the followin script goes with each of one of the gameObjects
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour
{
public string myWords = string.Empty;
private void Start ()
{
}
private void Update ()
{
}
public string GetWords ()
{
return myWords;
}
}
the follinw script is for the mainCamera it allows you to click on a model gameObject in the game and show the text attached for each obj, if you click on the mainCamara background no GUI is displayed! using UnityEngine; using System.Collections;
public class UI_Demo : MonoBehaviour
{
private GameObject clicked = null;
private void Start()
{
}
private void Update ()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(Camera.mainCamera.ScreenPointToRay(Input.mousePosition), out hit))
{
if (hit.collider.gameObject.GetComponent<Character>() != null)
clicked = hit.collider.gameObject;
else
clicked = null;
}
else
clicked = null;
}
}
private void OnGUI()
{
if (clicked != null)
GUI.Label(new Rect(15,15,200,25), clicked.GetComponent<Character>().GetWords());
}
}
i need help i'm in the process of learning C# and i'm stuck and in a hurry please help me how to figure it out and solve this ): sorry for the bad english ..
Answer by tw1st3d · Aug 01, 2013 at 05:42 AM
using UnityEngine;
using System.Collections;
public class OpenMenuButton : MonoBehavior
{
/* Set this script on your actual game object */
protected bool showMenu = false;
// Set showing the menu to false
void OnMouseUp()
{
// When you click, change the variables value
if(showMenu)
showMenu = false;
else
showMenu = true;
}
void OnGUI()
{
if(showMenu)
// Your GUI Code
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Bringing window to back GUI.BringWindowToBack() 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Card matching game 1 Answer