Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 GameDevH2O · Jul 27, 2021 at 05:06 PM · uiarrayliststringelements

How To Assign List Elements To Array Elements?

Hello, ive stumbled across an issue im not sure how to solve dealing with UI so I have a list of strings & an array of game objects that finds all UI gameobjects with a tag. what I need assistance with is how do I assign the first element of the list to the first element of the array & so on

 public List<string> Names = new List<string>();
 public GameObject[] bars;
 public GameObject prefab, parent;
 public int index;

 void Start()
 {
     bars = GameObject.FindGameObjectsWithTag("Bar");
     index = PlayerPrefs.GetInt("BarCount");
     if(bars.Length < index)
     {
         //spawn certain number of bars (with names if possible)
         for (int i = 0; i < index - 1; i++)
         {
             var bar = Instantiate(prefab);
             bar.transform.SetParent(parent.transform, false);
         }   
     }

     for (int i = 0; PlayerPrefs.HasKey("SaveName" + i); i++)
     {
         Names.Add(PlayerPrefs.GetString("SaveName" + i).ToString());    
     }
 }

here is where I want the elements to be assigned to each other in order then display the string element to the assigned bar. I have a vertical layout group spacing each bar.

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

2 Replies

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

Answer by elingranath01 · Jul 27, 2021 at 05:47 PM

As long as you make sure the array is the same length as the list, it's as simple as just making a for loop to assign the values from the list to the array.

 for (int i = 0; i < array.Length; i++){
 array[i] = list[i];
 }
Comment
Add comment · Show 4 · 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 GameDevH2O · Jul 27, 2021 at 10:38 PM 0
Share

Thank You For Your Expertise.

avatar image rage_co GameDevH2O · Jul 28, 2021 at 03:34 AM 0
Share

There is a much simple work around here....you just have to do

 array = list.ToArray();

simple as this...

avatar image elingranath01 rage_co · Jul 28, 2021 at 11:24 AM 0
Share

Yes, but GameDevH20 said they wanted to assign a list of strings to an array of gameobjects, not sure in what way they want to assign the list but I'm assu$$anonymous$$g they want to do gameObjectArray[i].name = nameList[i]; Seems like an odd way of assigning names when you can just do it in the inspector but I assume this is what GameDevH20 was wondering about

Show more comments
avatar image
0

Answer by Campbell52 · Jul 28, 2021 at 10:15 AM

First, you can convert array to ArrayList using 'asList ()' method of ArrayList. Add an element to the ArrayList using the 'add' method. Convert the ArrayList back to the array using the 'toArray()' method.

mygroundbiz

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

229 People are following this question.

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

Related Questions

C# ArrayList match to string? 1 Answer

A node in a childnode? 1 Answer

How to check if the string is on a text file 0 Answers

images assigned to gameobjects by tag 2 Answers

How do I check for strings in an array to match up with the index of another array? 1 Answer


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