Answer the question
In order to leave comments, you need to log in
How to overwrite array elements in List?
Good day. I'm making a game that has categories of questions. The player can choose categories independently, any. I can't make it so that multiple categories are added to the List<>. Variables are simply overwritten. And how to make the array not overwritten, but simply replenished?
That's what I'm doing:
public class Game : MonoBehaviour {
public CategoryList[] category = new CategoryList[9];
[HideInInspector]
public Toggle toggleSports, toggleGeo, toggleHistory, toggleFilmTV, toggleTech, toggleAnimal, toggleStars, toggleMusic, toggleChildren, toggleAll;
public WordsList[] words = new WordsList[10];
List<object> wordList;
public void StartGame()
{
wordList = new List<object>(words);
// Проверяем какие категории отмечены
for (int i = 0; i < words.Length; i++)
{
if (toggleSports.isOn == true)
{
words[i].word = LangSystem.lng.sports[i];
}
if (toggleGeo.isOn == true)
{
words[i].word = LangSystem.lng.geo[i];
}
Debug.Log(words[i].word);
}
WordsGenerate();
}
[System.Serializable]
public class CategoryList
{
public string[] catName = new string[9];
public WordsList[] words;
}
[System.Serializable]
public class WordsList
{
public string word;
}
}
Answer the question
In order to leave comments, you need to log in
"And how to make the array not overwritten, but simply replenished?"
The array cannot be replenished, it has a fixed size, which is set when it is created.
Instead of an array, use List.
List.Add(category); - to add a category.
List.Remove(category); - to delete.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question