InvokeRepeating doesn't work (111120)

I have this script, and it gives me these errors. What is wrong?
Thanks!

Assets/MyClass.cs(9,37): error CS1519: Unexpected symbol `RandomValue’ in class, struct, or interface member declaration

Assets/none.cs(17,17): error CS0178: Invalid rank specifier: expected ,' or ]’

using UnityEngine;
using System.Collections;

public class MyClass : MonoBehaviour {

	GameObject[] non;
	int i = 0;

	InvokeRepeating("Activator", 0.1f, 0.1f);

	void Activator() {
		i++;
		if(i >= non.Length)
		{
			i = 0;
		}
		non[!=i].SetActive(false);
	}
}

2 Answers

2

Invoke repeating is a method, and must be called from within a method or property. Try moving it into start.

Nice, it fixed the first problem! It is weird, because in Js it doesn't need to be inside a function Do you know why the second problem happens though?

–

'!=i' isn't valid for an array index. did you mean 'i'?

–

To achieve the 'everything else' index you need to use a loop to iterate through the full array. JavaScript allows a lot of lazy things. Eventually it always gets people into trouble. C# is stricter, which causes less headaches in the long term.

–

Yes, I try to achive the everything else. How will I go about creating that loop?

–

I managed to do this, but it stops after one cycle: #pragma strict var non : GameObject[]; function Change() { for (var x : int = 0; x < non.Length; x++) { non[x].SetActive(true); for(var y : int = 0; y < non.Length; y++) { Debug.Log(x + " " + y); if(y != x) { non[y].SetActive(false); } } if(x >= non.Length) { x = 0; } yield; } } function Update() { Change(); }

–

maybe something like this would work for you:

public GameObject[] non;
private int index = 0;

void Update()
{
    Change();
}

private void Change()
{
    index++;

    if (index >= non.Length)
    {
        index = 0;
    }

    for (var i = 0; i < non.Length; i++)
    {
        non*.SetActive(i == index);*

}
}
this is c# - the second piece of code you posted is unityscript, whereas the first was c#.