- Home /
GetComponents<> only returning first component
Hello. I've been experimenting with a trigger system for my game. However, my code has recently started returning only the first component of GetComponents<> instead of the whole list of components. Here is the relevant code loop.
foreach (TriggerEffect Effect in GetComponents<TriggerEffect>()) {
Debug.Log(Effect);
if (Effect.IsObjectiveEffect) return;
times.Add(Effect.Activate());
if (times[times.Count-1] == -1)
WaitForReturn++;
}
Whenever I switch my components around, it will always return the first TriggerEffect, depending on their order in the editor.
Does anyone have any idea why this would be happening?
Answer by Chimer0s · Feb 12 at 01:06 PM
Is the first one found always "IsObjectiveEffect?" If so, you're ending the loop when you tell it to return so it won't look through the others. You might want to use a break there instead. The rest of the code looks sound, so that's the only thing jumping out at me.
Right, would be my first guess as well. An early exit is an early exit
Makes sense. I realized that after doing some tests with debug statements. Also I believe it is continue, not break, which I think would also stop the foreach.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Enable Child Component C# 1 Answer
Custom Inspector, Access built-in component icons? 2 Answers
Simple question about OnMouseButtonAsUp 1 Answer
Vehicle Enter Script Errors HELP! 0 Answers