- Home /
 
InvokeRepeating doesn't work
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);
     }
 }
 
              Answer by Kiwasi · Jul 18, 2014 at 11:31 AM
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?
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();
 }
 
                 Answer by gjf · Jul 18, 2014 at 01:02 PM
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[i].SetActive(i == index);
     }
 }
 
               this is c# - the second piece of code you posted is unityscript, whereas the first was c#.
Your answer
 
             Follow this Question
Related Questions
Invoke.Repeating doesn't really "care" about repeat time? 1 Answer
Is it possible to change Invoke Repeating rate on the fly? 3 Answers
Subtract one from health every five seconds. 1 Answer
InvokeRepeating affecting parts of script 1 Answer
HELP BCE0077: It is not possible to invoke an expression of type 'UnityEngine.Vector3'. 1 Answer