x


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.

more ▼

asked Aug 02 '11 at 09:24 AM

PaulBrasfield gravatar image

PaulBrasfield
56 6 7 8

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;

}

}

more ▼

answered Apr 15 '12 at 02:52 PM

ghost010 gravatar image

ghost010
30

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 02 '11 at 09:42 AM

Eric5h5 gravatar image

Eric5h5
103k 50 166 622

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 03 '11 at 08:57 AM

PaulBrasfield gravatar image

PaulBrasfield
56 6 7 8

Please don't post comments as answers. As I said, your code must be in a class that extends MonoBehaviour. Check your formatting.

Aug 03 '11 at 09:12 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x204
x91
x7

asked: Aug 02 '11 at 09:24 AM

Seen: 7454 times

Last Updated: Apr 15 '12 at 02:52 PM