• 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 mmangual_83 · Jan 03, 2014 at 11:44 PM · c#list

Trouble with my string list

I have been having trouble getting my program to print out individual elements of a list. I have been given a number of tips and suggestions that have helped me a lot, however I am still not getting the desired output. First I need to show the code that I am working on.

What I am currently using is an extension class that handles printing the list onto the screen.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class Extensions {
     /// <summary>
     /// Converts a list to a string
     /// </summary>
     public static string ConvertToString<T>(this List<T> list)
     {
         var output = string.Empty;
         foreach (var item in list)
         {
             output += item.ToString() + ",";
         }
         return output;
     }
     
 }


This is the list that I want to use:

 public static List<string> task1Gesture = new List<string>(new[]
     {
         "Tap",
         "Grab",
         "Grip"
     });


This is how I am using the list and the extension class

 string[] gestureOptions = {   Cube_DemoPhase.task1Gesture.ConvertToString<string>(),
                                   Cube_DemoPhase.task1Gesture.ConvertToString<string>(),
                                   Cube_DemoPhase.task1Gesture.ConvertToString<string>()
                               }; 


This diagram will show what I am getting on the screen:

alt text

Can anyone help me figure out what I need to do in order to get this right?

randomlist.png (6.3 kB)
randomlist.png (6.3 kB)
Comment
Add comment · Show 1
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 ArkaneX · Jan 04, 2014 at 12:30 AM 0
Share

If you initialize a generic list, there's no need to use new[] inside. It's enough to write:

 new List<string> { "Tap", "Grab", "Grip" }

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by ArkaneX · Jan 04, 2014 at 12:24 AM

Your extension method outputs all list elements as a single string, while you need to get separate elements from it. When you create gestureOptions array, you just pass to it three the same elements.

The easiest solution is to just fill the gestureOptions variable with proper values:

 string[] gestureOptions = { "Tap", "Grab", "Grip" };

But in case when you have to store these values in Cube_DemoPhase class as a generic list, then you can do:

 string[] gestureOptions = Cube_DemoPhase.task1Gesture.ToArray();

There's no need for your extension class at all...

EDIT, that I hope will finish the line of "array of strings problem"

After reading all the comments + all your other questions related to the same problem, my solution is below. Please note that it required LINQ, so add using System.Linq; on top of your class:

 string[] gestureOptions = { "a", "b", "c", "d", "e" };
 int[] indexes = Enumerable.Range(0, gestureOptions.Length).ToArray();
 for (int i = indexes.Length - 1; i > 0; i--)
 {
     var r = Random.Range(0, i);
     var tmp = indexes[i];
     indexes[i] = indexes[r];
     indexes[r] = tmp;
 }
 var index = 0;
 gestureOptions = gestureOptions.OrderBy(x => indexes[index++]).ToArray();

Now, in indexes array, you have the positions of your randomized strings...

And here's my advice: next time please describe your problem fully in one question, and don't be afraid that it is long. You decided to share your problem and code piece by piece, and that caused a lot of confusion and lost time. Believe me - if the first question related to this problem were detailed, you'd have your answer the day you posted it.

Please go through all the previous question and close them all. And don't forget to reward people who tried to help by upvoting their answers/comments.

Comment
Add comment · Show 19 · 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 HappyMoo · Jan 04, 2014 at 12:39 AM 0
Share

This. Upvoted.

avatar image mmangual_83 · Jan 04, 2014 at 12:40 AM 0
Share

@ArkaneX: I get errors when I do string[] gestureOptions = Cube_DemoPhase.task1Gesture.ToArray();

avatar image ArkaneX · Jan 04, 2014 at 12:41 AM 0
Share

@Happy$$anonymous$$oo - do you ever sleep? ;)

avatar image HappyMoo · Jan 04, 2014 at 12:48 AM 1
Share

You can also do this:

 string[] gestureOptions = {   Cube_DemoPhase.task1Gesture[0],
                                   Cube_DemoPhase.task1Gesture[1],
                                   Cube_DemoPhase.task1Gesture[2]
                               }; 


ArkaneX, I sleep between the seconds

avatar image ArkaneX · Jan 04, 2014 at 12:55 AM 1
Share

@Happy$$anonymous$$oo - you forgot task1Gesture in your comment with indexes.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

A node in a childnode? 1 Answer

C# Dividing Gameobject List by Half 1 Answer

C# Displaying a List in Series 2 Answers

C# how to create a Descending GUI List 1 Answer

C# Displaying List Elements in Multiples 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges