- Home /
C# error - A namespace can only types and namespace declarations
(edit: when i realized the error and fixed it, it showed another one, could someone please pix this? thank you) I am a newbie to C#, and i do not know what this error is all about, i am trying to edit some variable from the caracter motor script (the one that comes with the caracter controller standard asset) i got this script from elsewhere in this site, i do not know wich post. but anyway, here is the code, the error is right at the start where i declare boolean. (it is in c# ;) )
using UnityEngine;
using System.Collections;
bool enable = new bool();
bool destroy = new bool();
public class MotorTest : MonoBehaviour
{
CharacterMotor motor;
void Start()
{
motor = GetComponent<CharacterMotor>();
if(enable)
{
motor.jumping.baseHeight = 2.0f;
if (destroy)
{
Destroy(gameObject);
}
}
else
{
motor.jumping.baseHeight = 1.0f;
if (destroy)
{
Destroy(gameObject);
}
}
}
}
Answer by timohausmann · Jul 15, 2015 at 10:54 PM
You cannot create variables or objects outside of the class definition. You should declare your variables at the beginning of the class and create initial objects during Start() or Awake().
It should look like this:
public class MotorTest : MonoBehaviour
{
CharacterMotor motor;
bool enable;
bool destroy;
void Start()
{
motor = GetComponent<CharacterMotor>();
enable = new bool();
destroy = new bool();
...
Answer by CaKeMeaT · Jul 15, 2015 at 10:27 PM
should your enable and destroy bools be inside the class ?
This is it. In C# variables must be declared inside a class.