- Home /
GUIText which corresponds with a GUIBox
I'm making a game where a player clicks an object eg. bed and has to type what it is into a GUITextField. There are ten words per scene and I want to create a GUIBox in each which charts the entering of each word and when entered correctly/incorrectly the adjoining word changes to green/red. My code thus far is below. Thanks in advance. using UnityEngine; using System.Collections;
public class Bed : MonoBehaviour {
public bool clickedBed = false;
public string bed = "Type Here";
void OnGUI() {
if (clickedBed) {
bed = GUI.TextField (new Rect (200, 78, 200, 20), bed, 25);
}
}
void OnMouseUpAsButton() {
Debug.Log ("Mouse DOWN!!!");
clickedBed = true;
}
void Update() {
if (Input.GetKeyUp (KeyCode.Return)) {
clickedBed = false;
// TODO proces guess
}
if (Input.GetMouseButton (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo, 100.0f)) {
if (hitInfo.collider.gameObject.Equals(gameObject)) {
Debug.Log ("Mouse Clicked!!!");
clickedBed = true;
} else {
GameObject go = hitInfo.collider.gameObject;
while (go.transform.parent != null) {
go = go.transform.parent.gameObject;
if (go.Equals(gameObject)) {
Debug.Log ("Mouse Clicked!!!");
clickedBed = true;
}
}
//Debug.Log ("Hit: " + hitInfo.collider.gameObject);
//Debug.Log ("Should Hit: " + gameObject);
}
}
}
}
}
Unity Answers addresses single, specific technical questions to help you write your code. I don't see a specific technical issue in your question, and I don't see any attempt at the code that matches your title.
I wasn't sure how to begin. That code I posted is where players type the string in for the object. I don't know how to have that be called to a GUIBox and change color.
Answer by robertbu · May 10, 2014 at 03:35 PM
If your specific question is about changing color, as far as I know, you cannot change the color of a GUI.Box(). You can change the background texture, but if you were going to do that, you might as well use GUI.DrawTexture() instead, and use a red or a green texture. So that is the place to start. It would be best to have an array of boolean values that you could set to true when you wanted green. Example code. Put it on an empty game object in a new scene, initialize redTexure and greeTexture in the Inspector, and hit play:
#pragma strict
var redTexture : Texture2D;
var greenTexture : Texture2D;
var startRect = Rect(10,10,40,40);
var padding = 3;
private var state : boolean[] = new boolean[10];
function Start() {
state[3] = true;
state[5] = true;
state[8] = true;
}
function OnGUI() {
var rect = startRect;
for (var i = 0; i < state.Length; i++) {
var tex = (state[i]) ? greenTexture : redTexture;
GUI.DrawTexture(rect, tex);
rect.x += rect.width + padding;
}
}
Your answer
Follow this Question
Related Questions
Trying to Highlight text in order to copy and paste, but do not want the text to be editable. 1 Answer
Game Menus WIth Keyboard Input Control 1 Answer
(C#) A better way to limit actions to once per button press? 2 Answers
GetComponent, set boolean to true but it willn't revert 3 Answers
Touch Input with Event Trigger. How to handle with it properly. 0 Answers