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 vedvishal100 · Mar 17, 2017 at 07:00 PM · c#unity5liststring

Assign the name of string as name of predefined List

I am making a Roulette game in Unity 5.0. I have a list in C# where I store the bets the player places. I use the following code to retrieve the bets placed by the user. Bets1 is the list which saves the type of bet placed by the user like SUP0 is straight up bet on Number 0, SPLT1 is split bet on number 1 & 2. These SUP0 and SPLT1 are lists in which these numbers are added.

 for (int i = 0; i < Bets1.Count; i++)
     {
         string a = Bets1[i].listname;
 
         Debug.Log(a);
         //the output in console is SUP0 for Straight up bet on number 0
 
         for (int j = 0; j < a.Count; j++)
         {
             Debug.Log(a[i]);
         }
 }



The above code shows error on a.Count and a[i]. My question is how to assign the value of variable a to be used as name of predefined list.

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
0

Answer by Azrapse · Mar 17, 2017 at 07:11 PM

You don't show what is the element type of the Bets1 list, which would help understanding your code. Anyway, you seem to read the listname member of every element in that list, that is a string, and storing it in a. Then, for some reason you iterate over every character in a? First, a.Count fails because a is a string, and strings don't have the Count method. If you want to know the amount of characters in a string, that is .Length. Second, the Debug.Log(a[i]); instruction is wrong. You are using the iteration variable of the outer for loop (i), instead of the iteration variable of the inner for loop (j), so of course, that will lead to some IndexOutOfBoundsException when you try to read a position not contained in the string.

In general, you need to rethink this, and explain better what you are attempting.

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 NoseKills · Mar 18, 2017 at 02:09 PM

If I understand correctly, what you want to do is something like

 Int[] SUP0 = new int[] {0};
 Int[] SPLT1 = new int[] {1, 2};
 ...
 string a = Bets1[i].listname; 
 // a has the value SPLT1 so you want to get numbers 1 & 2

You could do this using "reflection" and fiddling with variable names, but that's not the right way to go 95% of the time. Instead use a dictionary.

 Dictionary<string, int[]> bets = Dictionary<string, int[]>();
 bets.Add("SUP0", new int[]{0});
 bets.Add("SPLT1", new int[]{1, 2});
  
 string a = Bets1[i].listname; 
 // a has the value SPLT1 so you want to get numbers 1 & 2
 int[] numbers = bets[a];
 

A dictionary is a collection where you can Add() key-value pairs and fetch those values later using a key, your bet name in this case.

Looking at the rules of roulette, "a bet" is a fairly complex thing and i dont think this is the best way to represent its properties. This way for example you need to make a separate int list with a separate id of every straight up bet even though all of them act just the same.

You are not asking butmy first instinct would be to make a Bet class and extend it to get different kinds of bets.

 public abstract class Bet {
     protected int betOnNumber;
     public Bet (int onNumber){
         betOnNumber = onNumber;
     }
     public abstract bool WinsWith(int spinnedNumber);
 }
 
 public class StraightUpBet {
     public StraightUpBet (int onNumber) : base(onNumber) { }
     public override bool WinsWith(int spinnedNumber) {
         return spinnedNumber == betOnNumber;
     }
 }
 
 public class SplitBet {
     public SplitBet (int onNumber) : base(onNumber) { }
     public override bool WinsWith(int spinnedNumber) {
         return spinnedNumber == betOnNumber || spinnedNumber == betOnNumber + 1;
     }
 }

So each bet knows how to calculate whether it won with the number that was spun. If using inheritance feels out of your scope, it might be better to use your system :)

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

How can I get just one componente, or except one, from a list? (string.Join) 0 Answers

NullReferenceException when adding String to list 1 Answer

Distribute terrain in zones 3 Answers

Random role assigning from string 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