A
A
Anton Vertov2017-07-08 09:56:33
C++ / C#
Anton Vertov, 2017-07-08 09:56:33

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

As a result, only the second category is recorded, and I need them not to be overwritten, but added. Please tell me how to do it? Thank you very much in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
ReWire_92, 2017-07-10
@1Frosty

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

Ask a Question

731 491 924 answers to any question