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 /
avatar image
0
Question by Corlash · Jan 02, 2017 at 11:37 AM · c#uicollections

c# change what array I am calling dynamically

Ok, I am calling in a 1D array to populate a dropdown list and I basically want to be able to change the array I am calling in from the other script with some sort of variable. How would I go about this? Would I need Dictionary?(Don't really understand them yet). Thank you for any help!

     void OnTriggerStay(Collider other) {
         if (other.tag == "Player") {
             //Key press to bring up menu
             if (Input.GetKeyDown (KeyCode.E)) {
                 Debug.Log("getting menu");
 
                 //Find the dropdown
                 Dropdown uiDropdown = GameObject.Find ("MachineDropDown").GetComponent<Dropdown> ();
                 //Get rid of old info
                 uiDropdown.options.Clear();
                 //Populate dropdown
                 for (int i = 0; i < 2; i++) { 
                     uiDropdown.options.Add (new Dropdown.OptionData (gameObject.GetComponent<MachineMenuScript>().(((VARIABLE NAME OF ARRAY)))[i])); 
                 }
 
             }
         }
     }
Comment
Add comment · Show 2
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 dpoly · Jan 03, 2017 at 12:34 AM 0
Share

What are the possible array names that would go in here? Where is the rest of the code to show the full problem?

avatar image Corlash dpoly · Jan 03, 2017 at 01:44 AM 0
Share

Sorry, here is an example of the arrays in a script. the values are set in the inspector for each item the script is attached to as they all do different things with the same items.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class $$anonymous$$achine$$anonymous$$enuScript : $$anonymous$$onoBehaviour {
 
 
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /// Steel Products
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
     //Assembler Parts
     public string[] parts;
     public string[] finishedParts;
 
 
     //Steel Parts
     public string[] steelSquareBlanks;
     public string[] steelRectangleBlanks;
     public string[] steelCircleBlanks;
     public string[] steelBentSquareBlanks;
     public string[] steelBentRectangleBlanks;
     public string[] steelBentCircleBlanks;
     public string[] steelCylinderTubing;
     public string[] steelSquareTubing;
     public string[] steelCutCylinderTubing;
     public string[] steelCutSquareTubing;
 

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dpoly · Jan 03, 2017 at 03:09 AM

You can't use 'variable names' but you can index into an array of arrays.

 var arrays = new string[][] { 
   steelSquareBlanks, steelRectangleBlanks, steelCircleBlanks // etc
 };
 var x = 1; // choose the steel rectangles
 var opt = new Dropdown.OptionData(gameObject.GetComponent<MachineMenuScript>().arrays[x][i])
 uiDropdown.options.Add(opt); 

I like to use var a lot to break expressions into pieces that are easier to read. This is just skeleton code to give the idea.

Comment
Add comment · 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
0

Answer by Creeper_Math · Jan 02, 2017 at 06:38 PM

First of all, instead of the "for (int i = 0; i < 2; i++), I would use a "foreach", which just loops around with a variable "optiontext" (default being "item") that is set to the data in the array input each time

 string[] OptionFiller = // The string array of options u want the dropdown menu to have
 foreach(string optiontext in OptionFillerArray) {
      uiDropdown.options.add(new Dropdown.OptionData(item));
 }

The way I would do it would be way to complex, and still requires you to manually input all the arrays into a function that you need to get. I don't know about Dictionaries, so I will leave that for someone else to answer if they want.

Comment
Add comment · 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
0

Answer by RobAnthem · Jan 02, 2017 at 06:58 PM

In your dropbox class

 public string[] dbList;

and your other class would have

     FindObjectOfType<dbClass>().dbList = otherArray;

or

 private string dbList[]
 
 public void populateList(string[] myList)
 {
     dbList = myList;
     for (int i = 0; i < myList.Length; i++)
     {
         //do stuff with array entries
     }
 }

Your other class file would have in it

 FindObjectOfType<dbClass>().populateList(otherArray);


To be 100% clear, C# DOES NOT support dynamic variable names, because var names are only as meaningful as the uncompiled code they exist in, the moment they hit collections, that variable name is meaningless.

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

3D arrangement of collection of gameobjects 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Add Listeners to array of Buttons 2 Answers

c# to access force with scroll bar 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