- Home /
error CS1520: Class, struct, with Sprites
Hi everyone.
Im having a big problem with sprites (SpriteManager http://www.anbsoft.com/middleware/sm2/) 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
Answer by syclamoth · Oct 25, 2011 at 03:31 AM
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.
Answer by peluche · Oct 25, 2011 at 10:20 PM
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?
Well, presumably you are using some kind of sprite-manager- this error suggest that you need an instance of the Sprite class to be able to do your work! Look up your sprite manager documentation, since I really have no idea how your specific system works. From the sounds of the error, you need something like
public Sprite mySprite;
at the top, and then you need to replace all your references to Sprite with references to mySprite.
But, as I said, I don't know how this library works, so you'll just have to check your documentation.
Answer by peluche · Oct 26, 2011 at 10:46 PM
Thx.
I chaged my algorithm and put the script in another object and assign the object who has the spritemanager and works :D
But I will check the documentation to know more about this error.
Greetings
Your answer
Follow this Question
Related Questions
Changing sprite properties at runtime 2 Answers
The name 'PackedSprite' does not denote a valid type ('not found') 3 Answers
SpriteManager 2 pixel perfect blurry and stretched 2 Answers
iOS SpriteManager 2 issue with backgrounds 1 Answer
How do I use the PlayAnim function in SpriteManager 2? 1 Answer