how to loop a script

function Start () {
Repeat();

function Repeat();
	gameObject.tag = "Army Cube";
	yield WaitForSeconds(1);
	gameObject.tag = "Ice Cube";
	yield WaitForSeconds(1); 
	gameObject.tag = "Fire Cube";
	yield WaitForSeconds(1);
	gameObject.tag = "Mini Cube"; 
	yield WaitForSeconds(1);
	gameObject.tag = "Gravity Cube";   
	}

i have this code its suppose to loop but i keep getting this Error

.js(4,10): BCE0044: expecting (, found ‘Repeat’.

i did what it said but that just adds more errors i don’t know what to do

Your function named “function Repeat();” is located inside your Start() function. Notice the “Start() {” opens the Start function but it does not appear to close with “}” - at least not in the code you have shown.

Maybe add the “}” on the line just after “function Repeat();”, so it would look like this:

function Start () {
    Repeat();
}
function Repeat() {
    gameObject.tag = "Army Cube";
    yield WaitForSeconds(1);
    gameObject.tag = "Ice Cube";
    yield WaitForSeconds(1);
    gameObject.tag = "Fire Cube";
    yield WaitForSeconds(1);
    gameObject.tag = "Mini Cube";
    yield WaitForSeconds(1);
    gameObject.tag = "Gravity Cube";
}

That said, I think you may need to do a bit more reading to get a better grasp on this.

Edited to add that you’re also closing the Repeat() function before it ever gets started by adding the “;” right after it. see above sourcecode

It looks to me like you’re trying to create a coroutine, and not a simple for loop. Coroutines are pretty simple too though, so perhaps you mean to be doing the following:

function Start ()
{
    Repeat();
}

function Repeat()
{
    while (true)
    {
        gameObject.tag = "Army Cube";
        yield WaitForSeconds(1);
        gameObject.tag = "Ice Cube";
        yield WaitForSeconds(1);
        gameObject.tag = "Fire Cube";
        yield WaitForSeconds(1);
        gameObject.tag = "Mini Cube";
        yield WaitForSeconds(1);
        gameObject.tag = "Gravity Cube";
        yield WaitForSeconds(1);
    }
}

There are a lot of better was to write this though, like by using a modulus operation (written in C#, but you can copy/paste the whole codeblock into a file called LoopingTagAssigner.cs):

using UnityEngine;
using System.Collections;

public class LoopingTagAssigner : MonoBehaviour
{
	public float timer = 1;
	public string[] titles = new string[5]
	{
		"Army Cube",
		"Ice Cube",
		"Fire Cube",
		"Mini Cube",
		"Gravity Cube",
	};

	private int index;

	void OnEnable()
	{
		StartCoroutine(UpdateTitle());
	}

	IEnumerator UpdateTitle()
	{
		YieldInstruction wait = new WaitForSeconds(timer);
		while (enabled)
		{
			gameObject.tag = titles[index];
			index = ++index % titles.Length;
			yield return wait;
		}
	}
}

The advantages to using the above method is that the tags that you’re cycling through don’t need to be hardcoded in the script. However, an even better method would be to use a component to track the value instead of setting the tag (because there are a finite number of tags, and a game object can only have one tag at a time).

So you could try doing something like this:

using UnityEngine;
using System.Collections;

public class CubeType : MonoBehaviour
{
	public enum Type
	{
		Army,
		Ice,
		Fire,
		Mini,
		Gravity,
	}

	public Type CurrentType { get; private set; }

	void Awake()
	{
		numberOfTypes = System.Enum.GetValues(typeof(Type)).Length;
	}

	private int numberOfTypes;
	private int index;

	public Type CycleType()
	{
		CurrentType = (Type)System.Enum.ToObject(typeof(Type), index);
		index = ++index % numberOfTypes;
		return CurrentType;
	}
}

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CubeType))]
public class LoopingTagAssigner : MonoBehaviour
{
	public float timer = 1;

	private CubeType cubeType;
	private int index;

	void Awake()
	{
		cubeType = GetComponent<CubeType>();
	}

	void OnEnable()
	{
		enabled = cubeType != null;
		if (enabled)
			StartCoroutine(UpdateTitle());
	}

	IEnumerator UpdateTitle()
	{
		YieldInstruction wait = new WaitForSeconds(timer);
		while (enabled)
		{
			name = string.Format("{0} Cube", cubeType.CycleType().ToString());
			yield return wait;
		}
	}
}

I’m setting the name (purely so that you can see something happening when you hit play). You can query the current type easily by doing,

GetComponent<CubeType>().CurrentType;

All in all, these are contrived examples that demonstrate how you can use a couple of nice features. I wrote them in C# because I hate unityscript. :slight_smile: