x


Method containing while loop in custom class?

Hi! After a lot of testing, I'm posting... Trying to implement nice clean classes for a music game, one of them is a "local tempo" class. But it seems the method containing a while loop is simply not responding (no errors, nada). I tried to strip it down to the following:

    class Test
    {
         var testInt :int;
         function Test()
         {
              testInt =0;
         }
         function TestNow()
         {
              while(testInt<100)
              {
                   testInt++;
                   yield WaitForFixedUpdate;
              }
         }
    }

then instantiating the class and calling its method:

var test:Test;
test =new Test;
test.TestNow();

Debugging test.testint just gives me 0 forever... Is it just not possible to execute a while loop in a method? Or am I missing something super obvious?

Many thanks for your help!

Gregzo

more ▼

asked Dec 10 '11 at 10:02 PM

gregzo gravatar image

gregzo
1.6k 31 42 51

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Yields and coroutines can only be defined in a class deriving from MonoBehaviour. So you have to make Test derive from MonoBehaviour and place that onto a GameObject.

But the while-loop should work fine if you get rid of the yield in your current script.

more ▼

answered Dec 10 '11 at 10:06 PM

Daniel 6 gravatar image

Daniel 6
541 2 4 17

thanks! So, now in my class def, I type class Test extends MonoBehaviour , then use gameObject.AddComponent("Test") , to which I get an error : can't add because class Test does not exist... Hum!

Dec 10 '11 at 10:18 PM gregzo

What's the name of the script file you are defining it from? It needs to be called "Test" to work.

Dec 10 '11 at 10:23 PM Daniel 6

Argh, many thanks, that's it. It's all working now, although I realize I just wrote a script without using the shortcuts... Learning, cheers for the help!

Dec 10 '11 at 10:30 PM gregzo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x697
x349
x302
x62

asked: Dec 10 '11 at 10:02 PM

Seen: 743 times

Last Updated: Dec 10 '11 at 10:30 PM