- Home /
Can I create a list with an int/float and a string? C#
I'm creating a resource management game and want an easy way to access the resource amounts. I need a way so that each string is linked with an int/float, and can access and change the int/float amount. I have been googling for ever, and I haven't seen a clear answer or one that works properly.
Answer by CarpeFunSoftware · Aug 31, 2019 at 01:03 AM
You could also try a C# dictionary (generic), if your string is a key. It's implemented as a hash table and is very fast for searching. Create a class to hold your int/float (and maybe the string) values that correspond to the key.
Bit hard to tell from your description if that's what you want, but it should be pretty straight forward. It depends on if you're doing this in the editor or at run-time. SO's are in-editor stuff for creating entries in the asset database. If that's what you want, cool. If you want dynamic run-time data, Dictionary and/or List is probably worth looking into. You can serialize them with JSON or something. Serialization is a separate topic. But you can mark your classes [System.Serializable] and then select a serialization method for the collection(s).
https://www.youtube.com/watch?v=0WdWiF_Si4I
Summary: In-editor, use the SO like above to create assets in the database not bound to scripts. If dynamic and serialized in builds, or just runtime data, consider a plain class.
Answer by I_Am_Err00r · Aug 30, 2019 at 07:03 PM
You need a scriptable object (constructor outside of Unity):
using UnityEngine;
[CreateAssetMenu(fileName = "UniqueNameForAllTheDataThatWouldBeFormattedLikeThis", menuName = "UniqueNameForAMenu", order = 1)]
public class UniqueClassNameThatWillMakeSenseToYou: ScriptableObject
{
public string name;
public int whateverIntYouWant;
public float whateverFloatYouWant
}
Watch this video to understand what a scriptable object does and it will make sense in about 10 minutes.
Your answer
Follow this Question
Related Questions
Convert Text to float 3 Answers
cannot make a list with the word hello in it 1 Answer