x


[C#] Why does WaitForSeconds work with float defined but not variable?

Hello, I have a script that will eventually be used to create a jittery transform of scale,rotation etc.. for text on my main menu screen.

I have used a coroutine to create a looping scale (scale up and down repeatedly) I would like to attach this script to each letter in the words "Main Menu", so each letter will scale at a slightly different time than eachother.

So my thought was to create a public variable that I can specify a unique delay on the initial start of the coroutine for each letter. ("M" kicks off at 1/2 second, "A" at 1 full second , "I" at 1.5 seconds etc...

At the start of my script I define the following:

  // This is the time it takes to scale up/down and the delay before scale starts
 
     public float duration = 0.05f;
     public float waitdelay  = 1.0f;
     

If I use the following lines, the delay works in the script:

  public IEnumerator ScaleJitter () {
 
         yield return new WaitForSeconds (1.0f);

It works as expected, however if I change it to:

  public IEnumerator ScaleJitter () {
 
         yield return new WaitForSeconds (waitdelay);

It does not work. (Not only does it not pause, it does not run the coroutine to scale at all.

What am I missing?

more ▼

asked Mar 02 at 06:04 PM

fcm420 gravatar image

fcm420
3 1 3

Probably what robertbu said. But about your implementation, why not create a script with a coroutine which takes in the entire string, then iterate through each character and animate it?

Mar 02 at 06:44 PM A.Killingbeck
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Educated guess. Select the object with this script in the Hierarchy, and take a look at the value of 'duration' and 'waitdelay' in the Inspector. The value initialized in the top of the script is only used the first time the script is attached, or the first time the variable is declared. I'll bet the value for these variables is 0.0 in the Inspector.

more ▼

answered Mar 02 at 06:07 PM

robertbu gravatar image

robertbu
105k 19 37 114

You got it spot on! Thank you very much! So much headache for my morning this morning.

The value was actually set to 20.0f from I suspect the initial value I had in there when I attached the script.

Mar 02 at 06:59 PM fcm420
(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:

x637

asked: Mar 02 at 06:04 PM

Seen: 446 times

Last Updated: Mar 02 at 06:59 PM