Documentation index for AI agents (llms.txt) A Markdown version of every page is available: request the page's source under /docs/, follow the "alternate" link in this page's head, or start from /llms.txt.
Visual Script logo Manual

Foreach#

Control / Foreach

Description#

Loops a collection and runs the Instructions once for each of its items

Note

If the List iterated item is a component or a game object, Target references it

Parameters#

Name Description
Collection The collection of items to iterate
Instructions The Instructions that run once per item

Code#

using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Title("Foreach")]
    [Image(typeof(IconLoop), ColorTheme.Type.Teal)]

    [Description("Loops a collection and runs the Instructions once for each of its items")]
    [Category("Control/Foreach")]

    [Parameter("Collection", "The collection of items to iterate")]
    [Parameter("Instructions", "The Instructions that run once per item")]

    [Keywords("Loop", "For", "Each", "List", "Iterate", "Foreach", "While")]
    [Keywords("String", "Number", "Bool", "Variable", "Entry", "Item", "Element")]

    [NestOuterList(nameof(m_Instructions))]

    [Note("If the List iterated item is a component or a game object, Target references it")]

    [Serializable]
    public class InstructionCoreLoopForeach : Instruction, IParameterScope
    {
        // EXPOSED MEMBERS: -----------------------------------------------------------------------

        [SerializeField] private GetCollection m_Collection = new GetCollection();

        [SerializeField] private Instructions m_Instructions = new Instructions();

        // PROPERTIES: ----------------------------------------------------------------------------

        protected override string GetTitle(GameObject source) => $"Foreach {this.m_Collection} run {this.m_Instructions}";

        // RUN METHOD: ----------------------------------------------------------------------------

        protected override async Awaitable Run(Args args)
        {
            int count = this.m_Collection.Capture(args);
            if (count == 0) return;

            GameObject fallbackTarget = args.Target;
            using Args iterationArgs = Args.Get(args);

            for (int i = 0; i < count && !this.ParentCancelToken.Get; ++i)
            {
                this.m_Collection.BindParameter(iterationArgs, ArgsCollectionParameters.ITEM, i);
                ArgsCollectionParameters.SetIteration(iterationArgs, i, count);

                GameObject item = iterationArgs.GetParameterGameObject(ArgsCollectionParameters.ITEM);
                iterationArgs.SetTarget(item != null ? item : fallbackTarget);

                await this.m_Instructions.Run(iterationArgs, this.ParentCancelToken);
            }
        }

        public override void OnCancel()
        {
            base.OnCancel();
            this.m_Instructions.Cancel();
        }

        // PARAMETER SCOPE: -----------------------------------------------------------------------

        bool IParameterScope.ProvidesParameter(string member, PropertyName parameter)
        {
            return member == nameof(this.m_Instructions) && ArgsCollectionParameters.Contains(parameter);
        }
    }
}
Visual Script logoVisual Script © Catsoft Works 2026. All rights reserved.