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 Marshal_Alessi · Feb 23, 2016 at 07:33 AM · c#listfloat

Is there anyway to make a list of prewritten variables? (C#)

Hello, I'm in the process of writing up a script for a mock "Stock Exchange". However, in the game, I need them all to have starting values, so I typed them all out...

 public static float Bloxmart_S = 65.63f,KingSnackers_S = 38.27f,HurryNGo_S = 52.14f,FishySmells_S = 12.44f; 
     public static float McBlox_S = 117.64f,ChickenNFries_S = 68.41f,BlockoTaco_S = 71.89f;


To name a few. I have a function later on that will be used to change the stocks... However, the only way I know how to do this would be literally write out every single stock name by its float variable name to change it, which is both inefficient and I have somewhere close to 80 variables. I couldn't find a way to make a list containing all of these prewitten floats, and I found a few notes on reflection...

 float newVar = (float)this.GetType().GetField(StockNames[i]).GetValue(this);

(The StockNames[i] is a list of all the floaters names strings only). However, while that can find the variable, its duplicating it to the new variable, which means the original value stays the same. Is there anyway to actually find the float all contained within this one script by name?

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
2
Best Answer

Answer by Masterio · Feb 23, 2016 at 08:44 AM

Read about: Dictionaries, (Arrays and Lists too).

You can define all your data in a Dictionary:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Dictionaries : MonoBehaviour
 {
     public Dictionary<string, float> list;
     
     void Start()
     {
         // initialize new dictionary
         list = new Dictionary<string, float>();
         
         // add values to the list
         //PATTERN: list.Add("key", value);
         list.Add("Bloxmart_S", 65.63f);
         list.Add("KingSnackers_S", 38.27f);
         list.Add("HurryNGo_S", 52.14f);
         ///...
         
         // get value from the list by key
         float value;
         list.TryGetValue("HurryNGo_S",out value);
 
         // or
         value = list["HurryNGo_S"];
 
         // edit value by key name
         list["HurryNGo_S"] = 99f;
 
         // check if key exists
         if(list.ContainsKey("Bloxmart_S"))
             Debug.Log("Bloxmart_S exists.");
         else
             Debug.Log("Bloxmart_S not exists.");
 
         // check if value exists
         if(list.ContainsValue(65.63f))
             Debug.Log("65.63f exists.");
         else
             Debug.Log("65.63f not exists.");
 
         // remove key and value
         list.Remove("KingSnackers_S");
 
         // iterate over the list
         foreach(float v in list.Values)
         {
             Debug.Log(v);
         }
 
         // if you need a key and value
         foreach(KeyValuePair<string, float> p in list)
         {
             Debug.Log(p.Key + " - " + p.Value);
         }
     }
 }
 


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 Marshal_Alessi · Feb 23, 2016 at 07:09 PM 0
Share

Thank you, I hadn't even considered it...

avatar image
1

Answer by Q-ro · Feb 23, 2016 at 01:14 PM

why not use a dictionary ?

 //Add this to the top of your script
 using System.Collections.Generic;
 
 //then where you need to create your list (perhaps the player preferences) you define the dictionary as follows:
 
 var stocks = new Dictionary<string, string>();
 stocks["stock_name1"] = "value1";
 stocks["stock_name2"] = "value2";
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

101 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

Related Questions

Updating a List from a Custom Inspector 0 Answers

Firebase List Users 1 Answer

Get the lowest value from a Gameobject List 0 Answers

How Do I Create a List of Two Strings? C# 1 Answer

List keeps losing items. 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