- Home /
How to assign a variable individually for each element of an object list
for(int p = 0; p < galaxies.Count; p++)
{
for (int y = 0; y < Random.Range (1, maxSolarSystems + 1); y++) {
galaxies[p].solarSystems.Add (ss);
}
}
So I'll just start off with the code, I don't know how to correctly ask the question or if it's already posted and solved because I don't know how to phrase the question. Anyways, in the code above galaxies is a list that contains objects (a class called Galaxy) the size of it is determined randomly, thus far it works fine. Now in that class Galaxy there exists a variable called solarSystems, it's a list that contains also objects (class SolarSystem). Now I want to add a random amount of solarsystems to each galaxy instead of having the same amount of solarsystems for all galaxies, but if I do it this way it does take a random amount for each galaxy but adds them and gives each galaxy the added amount... It probably is something easy to fix but I just can't get it. (If you were wondering ss is just an instance of solarsystem)
Answer by Patrick2607 · May 09, 2017 at 09:15 PM
If you want solarsystem instances added to a galaxy, you need to create a new solarsystem every time, assuming that every solarsystem is a unique one.
galaxies[p].solarSystems.Add(new SolarSystem());
This will do the job I think. I don't know how you implemented the SolarSystem, but you get the idea. If the SolarSystem is deriving from MonoBehaviour, you'll need to Instantiate it in the Unity-way.
Thanks for the reply but that's exactly what I'm doing, ss is an instance of a new solarsystem, so that's not where the problem lies but you might be on to something, I never thought about the instances that get created for my galaxies nor my solarsystems, perhaps the problem lies in the fact that I created an instance Galaxy g = new Galaxy and use galaxies.add(g) so it's that same instance that gets added more than once to list ... thus explaining why the random amount of solarsystems added up ... you my kind sir just fixed my problem and made me realise I'm stupid... thanks for that ... No seriously thanks :D
I know ss
is an instance of a new solarsystem. But you will have to create a new instance every time you want a new solarsystem added to the list. Now you are just adding the same instance over and over again.
I know, I was doing exactly the same with my galaxies and that was what caused the problem. Tho not creating new instances for solarsystem didn't cause any problems at the moment it probably will later on, so I changed it as well. I get the logic behind it, I understand why it causes troubles I'm just baffled by the fact that I never even considered the instances that got created (or in my case, didn't get created). So thanks for your quick replies and help.
Thanks, it has to be done within half an hour tho :P So your quick replies might get me to finish it before the deadline :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612121649im_/https://answers.unity.com/themes/thub/images/avi.jpg)