Search Unity

Need help with Animation.Play

Discussion in 'Scripting' started by Miyyaa, Mar 30, 2015.

  1. Miyyaa

    Miyyaa

    Joined:
    Mar 30, 2015
    Posts:
    4
    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 ());
    }

    IEnumerator Go()
    {
    while(true)
    {
    Animation.Play ();
    yield return new WaitForSeconds(3f);
    }
    }

    }



    Animation auto-completes but the .Play after does not. The animation name is name is WallSpike. Really looking for some help here :(.
     
  2. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    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<Animation>().Play() and that will retrieve the animation attached to that class and play what it's set to :)
     
    Miyyaa likes this.
  3. Miyyaa

    Miyyaa

    Joined:
    Mar 30, 2015
    Posts:
    4
    That worked perfectly!!!! Thank you so much for the help and explaining why it was wrong :)
     
  4. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    You're very welcome--good luck! :)
     
  5. Miyyaa

    Miyyaa

    Joined:
    Mar 30, 2015
    Posts:
    4
    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.
     
  6. JustJeff

    JustJeff

    Joined:
    Feb 16, 2015
    Posts:
    84
    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).

    Something like this might be good for getting going in C# (though the second half sounds less than ideal for applying to Unity, it might be useful too):
    http://www.barnesandnoble.com/w/beg...54?ean=9781592005178&itm=1&usri=9781592005178

    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:
    http://www.amazon.com/Learning-Developing-Games-Unity-Beginners/dp/1849696586

    It's for a version ago but C# hasn't changed much. Good luck!