Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by IAmANewbie · Jan 29, 2017 at 01:07 PM · c#buttonarrayrandom

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IAmANewbie · Jan 29, 2017 at 04:07 PM 0
Share

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! ><

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a random string from array in C#? 1 Answer

Assigning a GameObject variable to equal another GameObject variable via C# script. 0 Answers

Random Generator Instantiate Tiles C# = PLEASE HELP = 0 Answers

Access GameObject Array and Turn Render on via the key pressed - Keys are already named based on game object. 0 Answers

Instantiated Buttons to have separate On-click functions 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges