Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCombat : MonoBehaviour

{

     public Animator animator;
     public Transform attackPoint;
     public float attackRange = 0.5f;
     public LayerMask enemyLayers;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)){

        Attack();
        }
    }

    void Attack()
    {

    animator.SetTrigger("Attack");

    Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

    foreach(Collider2D enemy in hitEnemies)
    {
    Debug.Log("We hit" + enemy.name);
    }
    }
}

void OnDrawGizmosSelected()

{


if (attackPoint == null)
     return;

Gizmos.DrawWireSphere(attackPoint.position, attackRange);

}


What I have tried:

I have tried everywhere to find help so i thought someone here could just give me the answer so i can build on it i am new to unity
Posted
Updated 16-Dec-20 1:43am
Comments
Richard MacCutchan 16-Dec-20 7:13am    
Well you could start by formatting your code so that all the indentations are correct and matched. Then you could tell us which line the error occurs on.
viewset too good 16-Dec-20 7:16am    
Yeah sorry i forgot to add which lines the errors are on its on line with void OnDrawGizmosSelected()
Richard MacCutchan 16-Dec-20 7:14am    
I assume the error is at OnDrawGizmosSelected, which is outside the class definition.
viewset too good 16-Dec-20 7:15am    
yes it is sorry i forget to say which lines it was on you are correct. I dont know what extra code i would have to add. Im new
Richard MacCutchan 16-Dec-20 7:18am    
The message is clear you cannot write a method outside of the class definition. If you are unsure of the correct syntax then study the language guide.

1 solution

The message says what it means: fields, properties and methods all have to be part of a class in C#, so you cannot declare them outside one.

What's happening is that you have a spurious close curly bracket immediately above the definition of
void OnDrawGizmosSelected()
which terminates the class definition. Remove it, or move it to the end of the actual class, and it'll probably work better.

If you check and maintain your indentation correctly, this kind of thing becomes a lot more obvious!
C#
    void Attack()
    {

    animator.SetTrigger("Attack");

    Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

    foreach(Collider2D enemy in hitEnemies)
    {
    Debug.Log("We hit" + enemy.name);
    }
    }
}

void OnDrawGizmosSelected()

{
Indent it, and it's more readable:
C#
   void Attack()
      {
      animator.SetTrigger("Attack");
      Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
      foreach(Collider2D enemy in hitEnemies)
         {
         Debug.Log("We hit" + enemy.name);
         }
      }         // <<< --- This ends the Atack function.
   }            // <<< --- So this ends the class.
void OnDrawGizmosSelected()
   {
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900