- Home /
List is empty in Update
So I have a list that I add objects to in another functions, and when I added the object the list count is correct. But when I loop through the list in the update it thinks the count is zero.
#region Members
//Array of all the particles
public Transform[] particles;
List<Transform> ap = new List<Transform>();
public int count = 0;
#endregion
public void UpdateEmitting()
{
count = ap.Count;
for (int i = ap.Count - 1; i >= 0; i--)
{
baseParticle bp = ap[i].GetComponent("baseParticle") as baseParticle;
if (bp == null)
Debug.LogError(" can't find baseParticle");
else
{
bp.UpdateEmitting();
bp.SetPosition();
if (bp.dead)
{
bp.StopEmitting();
ap.RemoveAt(i);
}
}
}
}
/// <summary>
/// Starts emitting particles
/// </summary>
/// <param name="name">Name of the emitter to start</param>
/// <param name="t">The transform to emit particles from</param>
public void StartEmitting(string name, Transform t)
{
foreach (Transform p in particles)
{
baseParticle bp = p.GetComponent("baseParticle") as baseParticle;
if (bp != null)
{
if (bp.name == name)
{
ap.Add((Transform)Instantiate(p, t.position, t.rotation));
bp.StartEmitting();
if (t != null)
bp.emitPosition = t;
Debug.Log("add count:" + active.Count);
return;
}
}
}
Debug.Log("No emitter found");
}
public void StartEmitting(string name, Vector3 pos, Quaternion rot)
{
foreach (Transform p in particles)
{
baseParticle bp = p.GetComponent("baseParticle") as baseParticle;
if (bp != null)
{
if (bp.name == name)
{
ap.Add((Transform)Instantiate(p, pos, rot));
bp.StartEmitting();
bp.SetPosition(pos, rot);
bp.emitPosition = null;
return;
}
}
}
Debug.Log("No emitter found");
}
/// <summary>
/// Stop emitting particles
/// </summary>
/// <param name="name">Name of the particle to stop emitting</param>
public void StopEmitting(string name)
{
for (int i = ap.Count - 1; i >= 0; i--)
{
baseParticle bp = ap[i].GetComponent("baseParticle") as baseParticle;
if (name == bp.name)
{
bp.StopEmitting();
ap.RemoveAt(i);
}
}
}
Can you add some debug output and run your script to help us figure out what you're doing? I don't see any update or start functions, so I don't know where you're entering this series of functions and what order things are firing in. With your debug, can you output the count of ap at each step?
The update is called from a $$anonymous$$onoBehaviour update call in another script. The ap.count during the updateEmitter it's always zero. But in the StartEmitting and StopEmitting the count is always correct to how many objects is added in the list.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
What do you think of this? List, Vector 3 and Update 1 Answer
Updating Array inside a List 1 Answer
List/Array Not Updating 2 Answers
Javascript Endless 3D Array 0 Answers