- Home /
Array of a class deleting previous array element
I add a new entry to the array with this...
theserver = new TheEnergySevers[currentServer + 1];
theserver[currentServer] = new TheEnergySevers();
theserver[currentServer].freqNumber = ServerNumber;
theserver[currentServer].addPerSec = addMount;
theserver[currentServer].totalAmountCap = Max;
currentServer += 1;
but it deletes the previous element of the array entry for example.
if I add values to the 2 element of the array it makes all the other array element equal o or null.
thanks in advance!
Answer by dmg0600 · Sep 22, 2014 at 09:51 AM
That is because of your first line: theserver = new TheEnergySevers[currentServer + 1];
Doing this you are overriding the array and erasing all previous values. It seems like you want to use a list.
Wherever you initialize the array put this instead:
List<TheEnergyServers> theserver = new List<TheEnergyServers>();
And change your code for adding a new server to this:
TheEnergyServers newServer = new TheEnergyServers();
newServer.freqNumber = ServerNumber;
newServer.addPerSec = addMount;
newServer.totalAmountCap = Max;
theserver.Add(newServer);
Remember that to use lists you have to add "using System.Collections.Generinc;" at the beginning of the script and that to know the number of elements in the list it goes like "theserver.Count" instead of "theserver.Length".
Your answer
Follow this Question
Related Questions
Static array with custom class? 1 Answer
Help with sorting values from a class 2 Answers
Script wont read array 2 Answers
JSON missing arrays 1 Answer
Unity JS array custom sort 2 Answers