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*).GetValue(this);*

(The StockNames 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?

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
			Debug.Log("Bloxmart_S exists.");
			Debug.Log("Bloxmart_S not exists.");

		// check if value exists
			Debug.Log("65.63f exists.");
			Debug.Log("65.63f not exists.");

		// remove key and value

		// iterate over the list
		foreach(float v in list.Values)

		// if you need a key and value
		foreach(KeyValuePair<string, float> p in list)
			Debug.Log(p.Key + " - " + p.Value);

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";