- Home /
Posting about a specific compiling error
I keep getting compiler errors even though my script has no errors?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float movespeed;
public float jumpforce;
private Rigidbody2D myRigidbody;
private Collider2D myCollider;
public bool grounded;
public LayerMask whatIsGround;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D > ();
myCollider = GetComponent<Collider2D> ();
}
// Update is called once per frame
void Update () {
grounded = Physics2D.IsTouchingLayers (myCollider,whatIsGround);
myRigidbody.velocity = new Vector2(movespeed,myRigidbody.velocity.y);
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) )
{
if (grounded)
{
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpforce);
}
}
}
It's difficult to tell due to how you pasted the script, but it looks like you may be missing a '}' at the end of your class.
The script looks fine to me!! Can you show the Error $$anonymous$$essage?
Answer by SohailBukhari · Jul 04, 2017 at 09:35 AM
Class brace is missing in your code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movespeed;
public float jumpforce;
private Rigidbody2D myRigidbody;
private Collider2D myCollider;
public bool grounded;
public LayerMask whatIsGround;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myCollider = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
myRigidbody.velocity = new Vector2(movespeed, myRigidbody.velocity.y);
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpforce);
}
}
}
}
Answer by Kingofweirdos · Jul 04, 2017 at 01:54 AM
check that your script has the same class as the file that you are putting on the object
It has to have the same name, if that is not the problem it may be other scripts or your animations
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How can I make a new line in Unity UI text? 2 Answers
"Nothinge Selected" 1 Answer