Error CS0116 "A namespace can only contain types and namespaces"?

I’ve stumbled upon a really frustrating error in my first project that I have no idea how to fix. “A namespace can only contain types and namespaces” in void Start and void Update. A little help would be appreciated.

using UnityEngine;
using System.Collections;

public class JimController : MonoBehaviour {

public float moveSpeed;

private Animator anim;

private bool playerMoving;
private Vector2 lastMove.x;
private Vector2 lastMove.y;

}
// Use this for initialization
void Start () { anim = GetComponent();}

// Update is called once per frame
void Update () {

	playerMoving = false;

	if(Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < 0.5f )
	{
	    transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
		playerMoving = true;
		lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    
    }
	if(Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f )
	{
		transform.Translate (new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
		playerMoving = true;
		lastMove = new Vector2(0f, Input.GetAxisRaw ("Vertical"));
	}
	anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
	anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
	anim.SetBool("PlayerMoving", playerMoving); 
	anim.SetFloat("LastMoveX", lastMove.x);
	anim.SetFloat("LastMoveY", lastMove.y);
}

Hi there @ChibiChubbs

It’s hard to tell as you’ve submitted your code in an extremely messy manner, please consider using the 101010 button to post your code next time so that it is properly formatted. Now, with that being said it looks like you’ve closed off your class with a curly brace just before the start method, consult below:

using UnityEngine; 
using System.Collections;

public class JimController : MonoBehaviour 
{
 public float moveSpeed;
 private Animator anim;
 private bool playerMoving;
 private Vector2 lastMove.x;
 private Vector2 lastMove.y;
} 

// Use this for initialization 
void Start () 
{ 
      anim = GetComponent();
}

 // Update is called once per frame
 void Update () 
{
     playerMoving = false;
     if(Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < 0.5f )
     {
         transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
         playerMoving = true;
         lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
     
     }
     if(Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f )
     {
         transform.Translate (new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
         playerMoving = true;
         lastMove = new Vector2(0f, Input.GetAxisRaw ("Vertical"));
     }
     anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
     anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
     anim.SetBool("PlayerMoving", playerMoving); 
     anim.SetFloat("LastMoveX", lastMove.x);
     anim.SetFloat("LastMoveY", lastMove.y);
 }

This in turn means all of your methods are within the global scope and Unity is assuming that you are declaring a namespace. Simply move the brace so that it encloses the entire logic and you should be fine. Like so:

using UnityEngine; 
using System.Collections;
  
public class JimController : MonoBehaviour 
{
     public float moveSpeed;
     private Animator anim;
     private bool playerMoving;
     private Vector2 lastMove.x;
     private Vector2 lastMove.y;
    
    
    // Use this for initialization 
    void Start () 
    { 
          anim = GetComponent();
    }
    
     // Update is called once per frame
     void Update () 
    {
         playerMoving = false;
         if(Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < 0.5f )
         {
             transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
             playerMoving = true;
             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
         
         }
         if(Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f )
         {
             transform.Translate (new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
             playerMoving = true;
             lastMove = new Vector2(0f, Input.GetAxisRaw ("Vertical"));
         }
         anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
         anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
         anim.SetBool("PlayerMoving", playerMoving); 
         anim.SetFloat("LastMoveX", lastMove.x);
         anim.SetFloat("LastMoveY", lastMove.y);
     }
}

I hope this helps! :slight_smile: