Trying to create a controller for a 2D playable character, but i get compilation error
Following a unity3D tutorial for creating a 2D character controller, i'm writing a piece of code but, after following it line by line and applying a modification to it due to changes from Unity 4 to Unity 5, i'm unable to try the code because i'm getting compilation error. I've looked into it time and time again but always get the same result. Can anyone help me, please?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroControl: MonoBehaviour
{
public float maxSpeed = 10f;
bool facingRight = true;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate()
{
float move = Input.GetAxis ("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if(move > 0 &&!facingRight)
Flip();
else if(move < 0 && facingRight)
Flip();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Answer by todinger · Aug 04, 2019 at 07:13 PM
You're missing a declaration of the variable 'anim'. After "bool facingRight = true;" try adding this line:
Animator anim;
Also in the future when you get a compilation error, try and find out what it means.
It's usually very descriptive (I'm guessing yours was something along the lines of not recognizing what 'anim' is).
If you can't and you decide to ask a question about it, then please always include the actual error text you get.
Good luck.
Answer by Osakasan · Aug 04, 2019 at 07:17 PM
Thanks! It works now :)
Sorry for not indicating the exact compilation error. I'm a total noob and didin't know they were that descriptive. I'll be more careful next time