Play Swimming Animation when in water. (ERROR CS0120)
So i've made a swimming script, that works fine. But i wan't to add, so when I'm in the water, it plays a swimming animation. I've made the animation, and put it into a script, but it says error CS0120: An object reference is required to access non-static member 'Unity.Engine.Play()'
NOTE I'm very new to scripting, just learning a little day by day how to do this and that. So i've probaly forgot to put something in the code. But here's the code:
using UnityEngine; using System.Collections;
public class Swimming : MonoBehaviour {
//under water for (useless atm)
void Start () {
RenderSettings.fog = false;
RenderSettings.fogColor = new Color (0.2f, 0.4f, 0.8f, 0.5f);
RenderSettings.fogDensity = 0.04f;
}
bool IsUnderwater()
{
return gameObject.transform.position.y < 3.9;
}
//swimming script
void Update () {
if (this.transform.position.y < 3.9)
{
this.gameObject.transform.position = new Vector3 (transform.position.x, 3.9f, transform.position.z);
RenderSettings.fog = IsUnderwater ();
//animation V
Animation.Play("SwimmingAnimation");
}
}
}
Everything else than the swimminganimation works.
Thanks :)
bENJAI
Answer by hexagonius · Sep 19, 2015 at 11:15 PM
Not
Animation.Play("SwimmingAnimation");
but
GetComponent<Animation>().Play("SwimmingAnimation");
Play cannot be statically used.
don't forget to add an Animation Component to the same gameobject this script is attached to.
Thanks dude :) But how do i add the Animation Component? Pretty new to the animation part :)!
when selecting the gameobject the inspector shows an AddComponent Button at the bottom
Your answer