• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PaulBrasfield · Aug 02, 2011 at 09:24 AM · namespacesyntax-error

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kapyar · Apr 02, 2016 at 02:58 PM 0
Share

In my case it was uncommented word class in the code

3 Replies

· Add your reply
  • Sort: 
avatar image
4

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;

 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Aug 03, 2011 at 09:12 AM 0
Share

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

avatar image aballif · Apr 22, 2016 at 01:10 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aballif · Apr 22, 2016 at 01:10 AM 0
Share

Are you talking about JavaScript, or is there another language in Unity besides JavaScript and C#?

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unable To Find Solution For Errors 1 Answer

error CS0246: The type or namespace name `rect' could not be found. 1 Answer

C# Visual Basic Error 1 Answer

A namespace can only contain types and namespace declarations 2 Answers

namespace support in unity 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges