error CS1520: Class, struct, with Sprites

Hi everyone.

Im having a big problem with sprites (SpriteManager SpriteManager 2) and Unity…

I have this code:

using UnityEngine;
using System.Collections;

public class QuestItemsSpriteController : MonoBehaviour {

 Update () {
    StartDelay();
    	Sprite.PlayAnim(0);
    StartDelay();
    	Sprite.PlayAnim(1);
	StartDelay();
    	Sprite.PlayAnim(2);
}

IEnumerator StartDelay(){
	yield return StartCoroutine(WaitSeconds (3.0f));
}

 IEnumerator WaitSeconds (float delay) {
    float timer = Time.time + delay;
    while (Time.time < timer) {
        yield return null;
    }
}

}

But the compiler send me this error message:

error CS1520: Class, struct, or interface method must have a return type

Any solution???

Thx

Simple- you need to add the word ‘void’ before Update(), to declare that it doesn’t have a return type! (I know, it seems kind of counter-intuitive, but that’s just the way it works.)

void Update()
{
    // code goes here, gets run once a frame.
}

Of course, since you are planning to use IEnumerators inside of your start function, you probably want to use it like this, instead:

 IEnumerator Start () {
    yield return StartDelay();
    Sprite.PlayAnim(0);
    yield return StartDelay();
    Sprite.PlayAnim(1);
    yield return StartDelay();
    Sprite.PlayAnim(2);
}

Unless you want every frame to last for 9 seconds, you shouldn’t put delays inside of Update! If you use Start as an IEnumerator, that will all execute properly on the first frame, however if you instead want it to trigger when you meet some condition, make all of that into a separate Coroutine, by changing the name of it from Start to something else, like ‘PlaySpriteAnims’, and then calling

StartCoroutine(PlaySpriteAnims());

when you want it to start.

One other thing, you see that lovingly written bit of code ‘WaitSeconds’? There is an inbuilt function which does exactly that, called WaitForSeconds(float seconds)- you use it where you would have had

yield return StartCoroutine(WaitSeconds (3.0f));

by putting

yield return new WaitForSeconds(3.0f);

instead. I’m honestly not sure whether it’d be faster or not- but a good rule of thumb is to use inbuilt functions whenever you have the choice.

Ok, I did these changes.

And have the code like this.

using UnityEngine;
using System.Collections;

public class QuestItemsSpriteController : MonoBehaviour {

void Start(){
	
	StartCoroutine(PlaySpriteAnims());
}


 IEnumerator PlaySpriteAnims () {
	 yield return new WaitForSeconds(3.0f);
	 Sprite.PlayAnim(0);
	 yield return new WaitForSeconds(3.0f);
	 Sprite.PlayAnim(1);
	 yield return new WaitForSeconds(3.0f);
	 Sprite.PlayAnim(2);
}

}

And have this error:

error CS0120: An object reference is required to access non-static member `Sprite.PlayAnim(UVAnimation_Multi)’

Options?

Thx.

I chaged my algorithm and put the script in another object and assign the object who has the spritemanager and works :smiley:

But I will check the documentation to know more about this error.

Greetings