A namespace can only contain types and namespace declarations (26052)

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.

In my case it was uncommented word class in the code

3 Answers

3

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;

}

}

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#?

Physics will work only if you put a collider on it. To detect UI elements you have to use GraphicRaycaster.

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 MonoBehaviour. Check your formatting.

If you look closely, you don't have most of your code inside the public class EnemyAI : MonoBehaviour 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.

Give each image its own BoxColider