- Home /
Ello, i'm new to unity but I keep getting this error in my script?
using UnityEngine; using System.Collections;
public class character : MonoBehaviour {
//movement variables
public float maxspeed;
Rigidbody2D myRB;
Animator myAnim;
bool facingRight
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D> ();
myAnim = GetComponent<Animator> ();
facingRight = true;
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");
myAnim.SetFloat ("speed", Mathf.Abs (move));
myRB.velocity = new Vector2 (move * maxspeed, myRB.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;
}
}
Unexpected symbol `void' in class, struct, or interface member declaration was the error, when I posted this I thought I had posted that as well
Would help if you tell us what the error is, but at least you are missing ; after bool facingRight.
Also you have exactly same condition in if and else if :)
Unexpected symbol `void' in class, struct, or interface member declaration was the error, when I posted this I thought I had posted that as well
Sometimes Notations can cause errors, try removing the unity-generated notations and running it.
Answer by Paul17041993 · Dec 10, 2016 at 04:12 AM
You're missing a semicolon ( ';' ) at the end of "facingRight", so the compiler thinks the "void" statement at the start of the "Start" function is continuing on the same line.
+1, the rest of us should of actually put it in a compiler before commenting :P
Answer by aditya · Dec 09, 2016 at 08:56 AM
you haven't post the error so this is just a guessing game and my guess is that the below line is the culprit
myAnim.SetFloat ("speed", Mathf.Abs (move));
because SetFloat
takes float as argument and you are using Mathf.Abs
which returns an int .... you can try replacing that line with below line
myAnim.SetFloat ("speed", (float)Mathf.Abs (move));
Answer by RobAnthem · Dec 09, 2016 at 10:50 AM
I'm going to guess perhaps his axis always = 0? As it should look more like.
float move = Input.GetAxis("Horizontal") * speed;
Your answer
Follow this Question
Related Questions
Help with script 1 Answer
ANSWERED Unexpected symbol 'void' - I am new to coding please help me find the error 1 Answer
help please 2 Answers
Weird Error c# 1 Answer
Unexpected symbol `void' in class, struct, or interface member declaration 0 Answers