- Home /
Instantiate gui.Text issue
Hi,
I am facing an issue, which i have been trying to solve for the past few hours, but no luck. My problem:
I have an interface with 5 buttons, each with their unique "id" set, which retrieves information from an data file and shows the data on the Gui.Text prefab i have setup.
Prefab: a guitext component named itemText
What it doesn:
It creates a set of buttons made from the arrayList and shows them correctly on the GUI - clicking one of those buttons, retrieves the information and displays it correctly. Although when i click another button in the same session, it seems it has duplicated the info from the first button, even though the data and the button "id" are unique!
What i'd like it to do is that when you click on another button, the "previous" object is overwritten, is there any way todo this ?
I tried destroying the prefab before initializing / on startup and when there is already some "text" retrieved, no luck on that :(
This is the code i have (some methods are removed)
public class ItemSelection : MonoBehaviour {
float ScreenWidth; float ScreenHeight;
public string item_name; public GameObject itemText;
IEnumerator getItems(int id){ // www classs data = update_get.text; GetLevel(dataArray); ShowInfo(id); }
void ShowInfo(int item_id){ GameObject itemName_GUI = Instantiate(itemText) as GameObject;
itemName_GUI.guiText.text = item_name;
itemName_GUI.guiText.fontSize = 14;
itemName_GUI.guiText.pixelOffset = new Vector2(-250, 200);
}
void OnGUI() { ScreenWidth = (Screen.width/2); ScreenHeight = (Screen.height/2); int i;
ArrayList arrayItems = new ArrayList();
arrayItems.Add("Pie");
arrayItems.Add("Apple");
arrayItems.Add("Cookies");
arrayItems.Add("Chickens");
arrayItems.Add("Horses");
int buttonHeight = -375;
for (i = 0; i < arrayItems.Count; i++){
buttonHeight = buttonHeight+60;
string name = arrayItems[i] as string;
if (GUI.Button( new Rect(ScreenWidth-500,ScreenHeight-buttonHeight,250,50), name)){
StartCoroutine(getItems(i));
isEmpty = 0;
}
}
}
}
Hi,
What do you mean exactly with this?
""previous" object is overwritten"
What i mean is: When the object is istantiated and the item_name is put in place when the first button is clicked, i want that value to be removed when the 4th button (for example) is clicked. So it only shows up once and not "layer above layer" :)
Answer by Uzquiano · Apr 24, 2011 at 09:32 PM
So, what is happening is what you wrote. You are creating a new object every time with this line
GameObject itemName_GUI = Instantiate(itemText) as GameObject;
So what you have to use is create just once that gameObject, and then only update this
itemName_GUI.guiText.text = item_name;
Then the text will change.
My suggestion is:
1 Do this at the very begining
GameObject itemName_GUI = Instantiate(itemText) as GameObject;
I understand what you are trying to say, i tried putting "GameObject itemName_GUI = Instantiate(itemText) as GameObject;" outside a method but it got me an error - 'error CS0236: A field initializer cannot reference the nonstatic field, method, or property'
Putting it inside let's say Start() doesn't work either, because it can not be accessed. PS: i am writing the code in C#, not Js
Yep, you should declare it outside everything, at the verybegining; but then inside the start() you should do the Instantiate()...
And since the reference is rachable for all the script is ok, and yu initialize inside a function, so it works.
It should be something like: GameObject itemName_GUI;
function Start(){itemName_GUI = Instantiate(itemText) as GameObject;}
Sorry but I didn't program in C#
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
How to apply check on an instanced prefab 2 Answers
Two Unity GUI questions 2 Answers
How to make a monolgue? 1 Answer