- Home /
Trying to diagnose this For Loop issue, for instantiating from a list.
New to Unity/Programming, so sorry if this is a dumb question.
This function controls the spawning of weapon parts in a weapon customization menu. Its also the function i reference in my .AddListener, and delegate.
The idea is that using the dropdown.value function, I can set an integer to the index of the drop down, then using a for loop, I can iterate through the List that the barrels are kept in, and ensure that when the Index of the Dropdown changes, the correct index of the list is referenced, and then spawns the correct barrel.
Basically my thought process is:
Find the Index that the Dropdown is on.
Store that index, and run through the list the gameobjects are stored in.
When the Index of the List is Equal to the Index of the Dropdown, stop the Loop, and instantiate the gameobject that is in that index of the List. And when the Index of the List != the index of the drop down, destroy the current gameobject, and spawn the correct one.
Apologies if this isnt formatted correctly
void spawnBarrels(int value, List<GameObject> temp, Transform vec)
{
var index = barrelDropDown.value;
for (int i = index; i == barrels.Count; i++)
{
barrelListIndex = i; //doesnt seem like this is the correct way to do this
break;
}
if (barrelListIndex == index && maxSpawn == 0)
{
GameObject tempObj = Instantiate(barrelList[barrelListIndex]);
tempObj.transform.position = vec.position;
currentActiveBarrel = tempObj;
Debug.Log(
currentActiveBarrel.name);
maxSpawn += 1;
if (index != barrelListIndex)
{
maxSpawn = 0;
Destroy(currentActiveBarrel); //doesnt destroy when the barrel changes
}
}
//sorry my code is shit
}
Problems:
The barrels dont change when the drop down changes, but I can see the index's changing in the inspector.
The barrels do not destroy when the value's change.
Im kinda stuck on this, and im not sure exactly how to diagnose this, theres no errors, and Im banging my head against the wall. My theory is that im not quite understanding how the for loop works, but from everything I read/understand this should be working.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to reference a multidimensional list? 1 Answer
Why does this script freeze Unity 2 Answers
Randomize Text on 4.6 UI Button's 1 Answer
JS: Access sections of an array, without activating the rest 2 Answers