• 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 samaxi · Feb 05, 2014 at 07:09 PM · c#javascriptfps

CS0116 "A namespace can only contain type and namespace declarations"

I am getting this error but I don't understand why. It says it is on line 108. Here is my script please help me.

 using UnityEngine;
 using System.Collections;
 
 public class AISimple : MonoBehaviour {
     
     public float Distance;
     public Transform Target;
     public float lookAtDistance= 50.0f;
     public float chaseRange= 20.0f;
     public float moveSpeed= 1.0f;
     public float Damping= 6.0f;
     public float fov= 160.0f;
 
     public int damage= 5;
     public int range= 5;
     public int force= 5;
 
     public Renderer muzzleFlash;
     public Light muzzleLight;
     
     private RaycastHit hit;
 
     public int direction = transform.TransformDirection(Vector3.forward);
     public RaycastHit shot;
     
     
     public CharacterController controller;
     public float gravity = 20.0f;
     private Vector3 moveDirection = Vector3.zero;
     
     /*
  * Is player in line of sight?
  */
 
     void Awake(){
         muzzleFlash.enabled = false;
         muzzleLight.enabled = false;
         //allowfire = true;
         }
 
     bool LineOfSight ( Transform target  ){
         if (Vector3.Angle(target.position - transform.position, transform.forward) <= fov &&
             Physics.Linecast(transform.position, target.position, out hit) &&
             hit.collider.transform == target) {
             return true;
         }
         return false;
     }
     
     
     /*
   This ai will fly and move through objects inlcuding terrain!
 */
     void  Update (){
         
         if (LineOfSight(Target) == true && hit.transform == Target) // Target is player
         {
             
             //enemy sees you - perform some action
             
             // Gauge the distance to the player. Line in 3d space. Draws a line from source to Target.
             Distance = Vector3.Distance(Target.position, transform.position);
             
             // Turn yellow enemy is getting close. Will chase soon.
             if (Distance < lookAtDistance)
             {
                 
                 lookAt();
             }
             
             // Attack! Chase the player until/if player leaves attack range.
             if (Distance < chaseRange)
             {
                 chase ();
                 attack();
                 animation.Play("walk");
 
             }
         } 
         else 
         {
             //enemy doesn't see you
         }   
     }
     
     
     
     // Turn to face the player.
     void  lookAt (){
         // Rotate to look at player.
         Quaternion rotation= Quaternion.LookRotation(Target.position - transform.position);
         // Dampening will slow the turn speed of enemy.
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
         //  transform.LookAt(Target);
     }
 
     
     void  chase (){
         moveDirection = transform.forward;
         
         moveDirection *= moveSpeed;
         
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 }
 
     void  attack (){
     audio.Play();
     
     
     // Did we hit anything?
     if (Physics.Raycast (transform.position, direction, shot, range)) {
         Debug.DrawRay(transform.position, direction * range, Color.green);
         // Apply a force to the rigidbody we hit
         if (hit.rigidbody)
             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
         hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
     }
     
     muzzleFlash.renderer.enabled = true;
     muzzleLight.enabled = true;
     yield return new WaitForSeconds(2);
     muzzleFlash.renderer.enabled = false;
     muzzleLight.enabled = false;
     
     
     yield return new WaitForSeconds(2);
     //allowfire = true;
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Feb 05, 2014 at 07:11 PM

Your bracket on line 106 closes off your class. Move the bracket to the end of the file.

Comment
Add comment · Show 5 · 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 samaxi · Feb 05, 2014 at 07:19 PM 0
Share

I did that but than this came up.

The body of 'AISimple.attack()' cannot be an iterator block because 'void' is not an iterator interface type.

avatar image robertbu · Feb 05, 2014 at 07:44 PM 0
Share

In C#, anything that has a yield in it must have a type of IEnumerator:

 IEnumerator attack ()

Then to call it:

 StartCoroutine(attack());

But you have further problem with your code after you fix this problem. The way it is currently structured, it calls 'attack()' for every frame that Distance < chaseRange. You don't want that. You only want to call attack() if the coroutine is not already running.

avatar image samaxi · Feb 05, 2014 at 07:54 PM 0
Share

So how would I fix that? I'm new to C#

avatar image robertbu · Feb 05, 2014 at 08:17 PM 0
Share

A simple fix would be to set a flag. Create a boolean at the top of your class:

 bool attacking = false;

Then in Update() do:

 if (!attacking) {
     StartCoroutine(attack());
 }


Then at the top and bottom of attack:

 IEnumerator attack() {
     attacking = true;
     // Guts of the code go here'
     attacking = false;
 }
avatar image samaxi · Feb 05, 2014 at 08:28 PM 0
Share

Okaii thank you very much.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Help| Convert a javascript to C# 1 Answer

Randomly Generated Objects 1 Answer

What Am I Doing Wrong? Variable Names 3 Answers

Need help with bullet/gun scripting 3 Answers

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