Building tycoon game, need help storing variables into an array.
Hi guys, sorry if this has been asked I've been googling it the last few days and haven't exactly been able to find anything useful to help.
So, I'm building a tycoon game, still new to unity but progressing well but I've hit a road block.
Basically i need an array that i can constantly add strings to the last index of the array(etc so there's something at index 0 i want to add the next item to 1, then 2 etc) I do not know how big the array is. i can't quite figure this one out.
I tried using a list but got some weird results such as it keeping data from the last run in the editor. Example, i run the game in the editor input two strings to the list "test1", "test2" i stop the game and run it again in the editor and i add another "TEST" variable and it prints "test1", "test2" and "TEST".
If anybody has a better idea for storing a lot of string variables other then a list or array i would be thankful.
TLDR; I need something i can add strings into without knowing how many strings will be put into it.
Answer by Socapex · Dec 28, 2016 at 10:26 PM
You want a List (which is a common container often called vector).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
public class blee : MonoBehaviour
{
[NonSerialized] List<string> my_strings = new List<string>();
void Start()
{
my_strings.Add("flaaflee");
my_strings.Add("bleeblou");
my_strings.Add("cheers m8");
foreach (string x in my_strings) {
Debug.Log(x);
}
}
}
I just read the part about your serialization issues. If you make the list public
, or use [SerializeField]
, data will be saved. This happens with most types you can use in unity. I added the [NonSerialized]
attribute for you. This will prevent unity from saving the data.
Your answer
Follow this Question
Related Questions
How do check which enum value was selected? 2 Answers
All GameObjects list to a GameObject? 0 Answers
can't save values from another class to a list 1 Answer
Pick a random string from a string array C# 1 Answer
How to do 2d tile room assignments? 0 Answers