Hello I’m new to C# and was wondering if I could get some help. I’m going through a tutorial series right now and I’m having problems with part of my code in a script. I have set up the animation and the animation works but the script is to add a 3 second delay between animation reset. This is the error :
Assets/Scripts/Trap.cs(16,35): error CS0120: An object reference is required to access non-static member `UnityEngine.Animation.Play()’
currently this is my code :
using UnityEngine;
using System.Collections;
public class Trap : MonoBehaviour {
public float delayTime;
// Use this for initialization
void Start () {
StartCoroutine (Go ());
}
What your error is telling you is that it sees you’re calling Animation.Play() but you’re not calling it on any specific object’s animation so it doesn’t know what to do or play.
Try doing GetComponent().Play() and that will retrieve the animation attached to that class and play what it’s set to
While I have this thread going. Anyone know any good places to learn C#? Right now I’m using this tutorial series to make a game but if I run into a problem I tend to get stuck.
it-ebooks.info has many useful books… I had experience in several other similar languages so I tend to just look up how to do things that are different syntactically in C# (like enumerations and 2D arrays) but the best thing if you haven’t had experience with object-oriented programming in an object oriented language would be to go through a basic book.
It SUCKS and will be BORING but by the time you get through 300 pages on how to write things like a console based tic-tac-toe game or other programming classics like “Hello World” and “Guess the Number”, you’ll have a much better understanding of how the code in Unity3d fits together… for example, what it means that almost each game class implements MonoBehavior (and what implementing means).
If you want a much-less-general and extremely application focussed approach, I used this as a reference several times when I was figuring out how the basics work:
It’s for a version ago but C# hasn’t changed much. Good luck!