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);
}

No; all code in Unity is compiled and there's no difference in code behavior in a script compared to code in a DLL. Except for the Debug.Log thing.

–

1 Answer

1

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:

Thank you! I wish I had reputation points to reward you. I've spent days trying to fix my errors.

–

Hmm, I must be missing something: The instructions have me creating the CustomDebug DLL as a separate project to be compiled into the DLL. I don't see how I can reference any of the code from my Unity project, like say.. GlurthSettings.numberOfDoDadsInScene, when it's not defined in the DLL Project, without generating a compiler error (not in scope). Then again, I have not gone very far with this so far, perhaps it will become apparent when I do.

–