- Home /
C# Dividing Gameobject List by Half
I want to create a gameobject list that is equal to half of another gameobject list. But I'm not sure how to get it. First thing that came to mind was
gameobjectList2 = gameobjectList/2
but that doesn't work since that is using both a gameobject list and an integer. I also tried using a for loop
for(int i=0;i<gameobjectList.Count/2;i++)
gameobjectList2.add(gameobjectList[i])
but that gives me a list that is multiplied not divided. any idea how I can get gameobjectList2 to equal half of gameobjectList?
The GetRange code solved my problem. I knew it was something simple but none of the methods I tried worked. Thanks for the help.
No problem... I'll convert my comment to an answer so you can accept it!
Btw for future reference here's a link to the List documentation detailing the available methods: http://msdn.microsoft.com/en-us/library/s6hkc2c4(v=vs.90).aspx
Answer by Huacanacha · Jul 19, 2014 at 04:12 AM
Are you saying you have a list of GameObjects and you want a list with just half of those GameObjects in it? If so you could use GetRange...
List<GameObject> gameObjectList2 = gameobjectlist.GetRange(0, gameobjectlist.Count/2);
Your loop method should have worked if gameobjectList2 was empty before the loop. If this doesn't solve your problem post more of your code for context.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154446im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# Displaying List Elements in Multiples 1 Answer
C# Randomly Adding Elements from stringListA to stringListB 1 Answer
C# Adding Multiple Elements to a List on One Line 5 Answers
C# Displaying a List in Series 2 Answers