- Home /
Multiple technical questions
Gui list and color
I have a scene where I click an object and then type into a GUI Field which then saves the text in as a string. I now want that string to appear in a GUI list (along with other words) and when the input is correct have the text change color in the list. Any help would be great.
using UnityEngine;
using System.Collections;
public class BookTest : MonoBehaviour {
public bool clickedBed = false;
public GUISkin mySkin;
public string PlayerInput = "";
public string Word = "book";
public bool CorrectAnswer= false;
void OnGUI (){
// Assign a skin to you menu
GUI.skin = mySkin;
// Background box
GUI.Box ( new Rect(220,30,20,120), "Enter Answer");
//Text area for the username
PlayerInput = GUI.TextArea ( new Rect(220,30,100,20),PlayerInput);
// Loads level (0). This map can be set in the build settings
if (GUI.Button ( new Rect(100,20,60,25), "Enter Answer")) {
if(CorrectAnswer == true){
void Update (){
// Updates each frames and checks if the TextArea is equal to the username we want
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;
}
if (PlayerInput == Word){
CorrectAnswer = true;
}
else {
CorrectAnswer = false;
}
}
}
Your question is both over broad for a UA question, and you posted code that does not even compile. You cannot nest functions in C#, and your Update() function is nested inside your OnGUI() function. As for your question, you need to break it down into specifics, do some research, then if you get stuck, post back with a new question:
Displaying multiple lines.
$$anonymous$$odifying that list dynamically based on other input
Processing strings from a TextArea input
Changing string color
As for color, look at Unity's rich text tags.