- Home /
A namespace can only contain types and namespace declarations
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
I keep getting the namespace error on this line of code and I've tried to fix it for the past 2 hours but can't. A little help would be appreciated.
Answer by ghost010 · Apr 15, 2012 at 02:52 PM
All the code needs to be inside the MonoBehaviour { } brackets,or els it wont work,smartypants. its the same as HTML. it all needs to be inside the main block. thats how it works.
your code was outside of the MonoBehaviour {} brackets. as this was a simple cut and paste fix , i did the fixing for you. (if its still relevant)
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed;
void Start () { GameObject go = GameObject.FindGameObjectWithTag("Player"); target = go.transform;**
}
void Update () { Debug.DrawLine(target.position, myTransform.position, Color.yellow);**
//Look At Target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//Move Towards Target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
Answer by PaulBrasfield · Aug 03, 2011 at 08:57 AM
I know that but this is just a snippit of code. Here's the full code.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
}
**void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;**
}
**void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);**
//Look At Target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//Move Towards Target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
The lines that have the error gave the ** on them.
Please don't post comments as answers. As I said, your code must be in a class that extends $$anonymous$$onoBehaviour. Check your formatting.
If you look closely, you don't have most of your code inside the public class EnemyAI : $$anonymous$$onoBehaviour
brackets. That is the part of the code that actually gets runned and if you don't have your code in there you will get an error.
Answer by Eric5h5 · Aug 02, 2011 at 09:42 AM
You can't use C# like that; it must be in a class that extends MonoBehaviour (plus you need to import the UnityEngine namespace). If you want simpler coding that doesn't require that kind of boilerplate code, you'd be better off with Unityscript.
Are you talking about JavaScript, or is there another language in Unity besides JavaScript and C#?
Your answer
![](https://koobas.hobune.stream/wayback/20220612164408im_/https://answers.unity.com/themes/thub/images/avi.jpg)