C# Adding random values to array
Hello everyone (Sorry for my bad english ;_;)
I have a problem with adding random values to button that created from using for loop and array. The value in my button.text is wrong from the value in console like this picture >> https://goo.gl/gQb8m6
I think the value in console is a new random value from the function but I dont know how to fix it.
This is my code ( I think the problem is come from CreateButton() and RandomValue() ?? ) Please help me TT & Thank you
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class createBtn : MonoBehaviour {
public GameObject[] btnArr;
public Transform btnField;
public GameObject RedBtn;
public GameObject GreenBtn;
public GameObject destroyBtn;
public static int valueLogs;
public Text showValueLogRed, showValueLogGreen;
public static int[] BudgetValue;
void Start() {
CreateButton();
destroyBtn.SetActive(true);
}
//Function for CLICK Button
public void DestroyBTN() {
StartCoroutine(DestroyIt());
}
//Create 8 Button and Random Green/Red
public void CreateButton() {
btnArr = new GameObject[8];
BudgetValue = new int[8];
for (int i = 0; i < 8; i++) {
int randomTag = Random.Range(0, 20);
if (randomTag % 2 == 0) {
GameObject buttonHere = Instantiate(GreenBtn);
buttonHere.name = "Button " + i;
buttonHere.transform.SetParent(btnField, false);
btnArr[i] = buttonHere;
RandomValue();
showValueLogGreen.text = "" + valueLogs;
BudgetValue[i] = valueLogs;
print("Button " + i + "'s value = " + BudgetValue[i]);
} else {
GameObject buttonHere = Instantiate(RedBtn);
buttonHere.name = "Button " + i;
buttonHere.transform.SetParent(btnField, false);
btnArr[i] = buttonHere;
RandomValue();
BudgetValue[i] = valueLogs;
showValueLogRed.text = "" + valueLogs;
print("Button "+i + "'s value = " + BudgetValue[i]);
}
}
}
//Destroy Btn Fucntion for DestroyButton
IEnumerator DestroyIt() {
for (int i = 0; i < 8; i++) {
Destroy(btnArr[i]);
}
yield return new WaitForSeconds(2);
CreateButton();
}
//Random value in Button
public static void RandomValue() {
valueLogs = Random.Range(1, 10);
}
}
Answer by Bunny83 · Jan 29, 2017 at 02:42 PM
Well, you don't set the button text but the text of your prefab. The output you see makes perfect sense. Here's what happens:
//
| index | random | Green Prefab | RedPrefab | oucome
---------------------------------------------------------
Initial state | --- | --- | " 1 " | " 2 " | ---
Select Green | 0 | 7 | " 1 " | " 2 " | Green " 1 "
Select Red | 1 | 7 | " 7 " | " 2 " | Red " 2 "
Select Red | 2 | 3 | " 7 " | " 7" | Red " 7 "
Select Red | 3 | 6 | " 7 " | " 3" | Red " 3 "
Select Green | 4 | 4 | " 7 " | " 6" | Green " 7 "
Select Red | 5 | 9 | " 4 " | " 6" | Red " 6 "
Select Green | 6 | 8 | " 4 " | " 9" | Green " 4 "
Select Green | 7 | 3 | " 8 " | " 9" | Green " 8 "
final state | --- | --- | " 3 " | " 9 " | ---
What you actually do is: You instantiate the prefab in it's current state and set the text of the prefab afterwards. So the number you choosen for your first "Green" button will be seen on the second green button. Likewise the random number of your first red button will be seen on the second red button. The table might look confusing, just have a look on only one prefab color:
// Green prefab only
| index | random | Green Prefab | oucome
---------------------------------------------------------
Initial state | --- | --- | " 1 " | ---
Select Green | 0 | 7 | " 1 " | Green " 1 "
Select Green | 4 | 4 | " 7 " | Green " 7 "
Select Green | 6 | 8 | " 4 " | Green " 4 "
Select Green | 7 | 3 | " 8 " | Green " 8 "
final state | --- | --- | " 3 " | ---
// Red prefab only
| index | random | RedPrefab | oucome
-------------------------------------------------------------
Initial state | --- | --- | " 2 " | ---
Select Red | 1 | 7 | " 2 " | Red " 2 "
Select Red | 2 | 3 | " 7" | Red " 7 "
Select Red | 3 | 6 | " 3" | Red " 3 "
Select Red | 5 | 9 | " 6" | Red " 6 "
final state | --- | --- | " 9" | ---
So your solution is to either change the prefab before you instantiate it so the instantiated object will copy the current state of the prefab and has the right number on it.
Or you do what most would do: Get the Text component from your instantiated GameObject and set it's text.
So usually you would do:
GameObject buttonHere = Instantiate(GreenBtn);
buttonHere.name = "Button " + i;
buttonHere.transform.SetParent(btnField, false);
btnArr[i] = buttonHere;
Text textComp = buttonHere.GetComponentInChildren<Text>();
RandomValue();
textComp.text = "" + valueLogs;
BudgetValue[i] = valueLogs;
print("Button " + i + "'s value = " + BudgetValue[i]);
ps: Using a static variable just to return a value from a static method is a very bad idea. Why don't you just return the value? You can temporarily store the result it in a local variable.
Thank you so muchhh!! ;; Actually I just solve this problem by create the text together with the button(without text) and setParent to the button. However, your code is better than $$anonymous$$e! ps. I think about using these variable to the other script in the future so I use a static variable & I still confuse about a static variable and return value. I'll try to learn and understand it. Thanks for your advice! ><