NullRefException at Index 0 in list
So I have a Rhythm Controller (while using RhythmTool) and in this controller I have this public list called "items" that's apart of a class. Every item that is added to that list is manipulated by the for-loop below to move the object at the speed of the low frequency frames (Beat, bass, etc).
//move the objects at the speed of the frame.
foreach (ItemScript item in items)
{
for (int i = rhythmTool.CurrentFrame; i < item.Index; i++)
{
variableSpeed += low[i].magnitudeSmooth * .2f;
}
variableSpeed -= (low[rhythmTool.CurrentFrame].magnitudeSmooth * .2f) * rhythmTool.Interpolation;
position.y += variableSpeed;
item.gameObject.transform.position = vanishingPoint;
if (rhythmTool.CurrentFrame >= item.Index)
toRemove.Add(item);
}
foreach (ItemScript item in toRemove)
{
items.Remove(item);
item.gameObject.SetActive(false);
}
When I activate objects through a different class, I want to add them to the list so that they can be manipulated by the for loop above. I do so using this:
public void SpawnObject()
{
GameObject obj = ObjectPoolerScript.current.GetPooledObject();
if (obj == null) return;
obj.transform.position = transform.position;
obj.transform.rotation = transform.rotation;
RhythmController.current.items.Add(obj.GetComponent<ItemScript>());
obj.SetActive(true);
}
The problem I'm getting is that when I try to add an object to the items list, nothing happens at run time. The Item's list remains at 0 for its Count. However when I add one to the size of the list (or the Count variable). They are added to the list without any problem. Because of the code above though, I get a NullReferenceException due to there being object at the index of items.
How would I go about making sure that the first item added to the list is at the index from the start?