Please help me with this error: CS1525: Invalid expression term ‘[’"

I wrote some code following a tutorial and i had some errors i managed to solve them my self but there is one that stuck around.

This is the error:

Assets\EnemyAI 2.cs(179,59): error CS1525: Invalid expression term ‘[’

This is the part of the code that supposedly contains the problem:

void EnviromentView()
{
    Collider[] playerInRange = Physics,overlapSphere;([transform.position, viewRadius, playerMask]);
    for(int i=0;i<playerInRange.Length; i++)
    {
        Transform player = playerInRange[i].transform;
        Vector3 dirToPlayer = (player.position - transform.position).normalized;
        if (Vector3.Angle (transform.forward, dirToPlayer)< viewAngle / 2)
        {

if you know anything and think you can help please do.

4 Answers

4

Next time when you make a post if you could put any code in the correct format it would be appreciated, the icon to do that looks like </>

There’s a few issues that I can see from a quick look, ill list them below:

  • Theres a comma rather than a . after Physics
  • Theres a ; after overlapsphere
  • the brackets are not needed there

Here’s the code with those fixes:

private void EnviromentView()
{
    Collider[] playerInRange = Physics.OverlapSphere(transform.position, viewRadius, playerMask);
    for (int i = 0; i < playerInRange.Length; i++)
    {
        Transform player = playerInRange[i].transform;
        Vector3 dirToPlayer = (player.position - transform.position).normalized;

        if (Vector3.Angle(transform.forward, dirToPlayer) < viewAngle / 2)
        {

        }
    }
}

Thank you but i still get errors on the same line when i have fixed it, they are:

CS1026 ) expected 179, 58
CS1002 ; expected 179, 102
CS1513 } expected 179, 102

if i add them where unity tells me to i get 30 errors and i dont know what to do.

Could you send the whole script as the errors seem to be somewhere else as the answer I put above has no syntax errors, use the </> button to put your code in for better formatting, thanks :)

–
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyAI2 : MonoBehaviour
{
    public NavMeshAgent navMeshAgent;
    public float startWaitTime = 4;
    public float timeToRotate = 2;
    public float speedWalk = 2;
    public float speedRun = 9;

    public float viewRadius = 15;
    public float viewAngle = 90;
    public LayerMask playerMask;
    public LayerMask obstacleMask;
    public float meshResolotion =1f;
    public int edgeInteractions = 0.4;
    public float edgeDistance = 0.5f;

    public Transform[] waypoints;
    int m_CurrentWaypointIndex;

    Vector3 playerLastPosition = Vector3.zero;
    Vector3 m_PlayerPosition;

    float m_WaitTime;
    float m_TimeToRotate;
    bool m_PlayerInRange;
    bool m_PlayerNear;
    bool m_IsPatrol;
    bool m_CaughtPlayer;




    // Start is called before the first frame update
    void Start()
    {
        m_PlayerPosition = Vector3.zero;
        m_IsPatrol = true;
        m_CaughtPlayer = false;
        m_PlayerInRange = false;
        m_WaitTime = startWaitTime;
        m_TimeToRotate = timeToRotate;

        m_CurrentWaypointIndex = 0;
        navMeshAgent = GetComponent<NavMeshAgent>();

        navMeshAgent.isStopped = false;
        navMeshAgent.speed = speedWalk;
        navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex]);
        
    }

    // Update is called once per frame
    void Update()
    {EnviromentView();
    if(!m_IsPatrol)
    {
        Chasing();
    }
      else
      {
        patroling();
      }  
    }
private void Move(float speed)
    {
        navMeshAgent.isStopped = false;
        navMeshAgent.speed = speed;
    }
void CaughtPlayer()
    {
       m_CaughtPlayer = true; 
    }

    void Stop()
    {
        navMeshAgent.isStopped = true;
        navMeshAgent.speed = 0;
    }
    private void Chasing()
    {
        m_PlayerNear = false;
        playerLastPosition = Vector3.zero;

        if (!m_CaughtPlayer)
        {
            Move(speedRun);
            navMeshAgent.SetDestination(m_PlayerPosition);
        }
        if (navMeshAgent.ramainingDistance <= navMeshAgent.stoppingdistance)
        {
            if (m_WaitTime <= 0 && !m_CaughtPlayer && Vector3.Distance(transform.position, GameObject.FindGameObjectWithTag("Player".transform.position)>= 6f))
            {
                m_IsPatrol = true;
                m_PlayerNear = false;
                Move(speedWalk);
                m_TimeToRotate = timeToRotate;
                m_WaitTime = startWaitTime;
                navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
            }
            else
            {
                if (Vector3.Distance(transform.position, GameObject.FindGameObjectWithTag("Player").transform.position)>= 2.5f)
                {
                    Stop();
                    m_WaitTime -= Time.deltaTime;
                }
            }
        }
    }   
    private void patroling()
    {
        if (m_PlayerNear)
        {
            if (m_TimeToRotate <= 0)
            {
                Move(speedWalk);
                LokkingPLayer(playerLastPosition);
            }
            else
            {
                Stop();
                m_TimeToRotate -= timeToRotate.deltaTime; 
            }
        }
        else
        {
            m_PlayerNear = false;
            playerLastPosition = Vector3.zero;
            navMeshAgent.SetDestination (waypoints[m_CurrentWaypointIndex].position);
            if (navMeshAgent.ramainingDistance <= navMeshAgent.stoppingdistance)
            {
                if(m_WaitTime <= 0)
                {
                    NextPoint();
                    Move(speedWalk);
                    m_WaitTime = startWaitTime;
                }
                else
                {
                    Stop();
                    m_WaitTime -= timeToRotate.deltaTime;
                }
            }
        }
    }

    public void NewtPoint()
    {
        m_CurrentWaypointIndex = (m_CurrentWaypointIndex+1) % waypoints.Length;
        navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
    }
    void LookingPlayer(Vector3 player)
    {
        navMeshAgent.SetDestination(player);
        if(Vector3.Distance(transform.position, player) <= 0.3)
        {
            if (m_WaitTime <= 0)
            {
                m_PlayerNear = false;
                Move(speedWalk);
                navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
                m_WaitTime = startWaitTime;
                m_TimeToRotate = timeToRotate;
            }
            else
            {
                Stop();
                m_WaitTime -= timeToRotate.deltaTime;
            }
        }
    }
    void EnviromentView()
    {
        Collider[] playerInRange = Physics.overlapSphere([transform.position, viewRadius, playerMask]);
        for(int i = 0; i <playerInRange.Length; i++)
        {
            Transform player = playerInRange[i].transform;
            Vector3 dirToPlayer = (player.position - transform.position).normalized;
            if (Vector3.Angle (transform.forward, dirToPlayer)< viewAngle / 2)
            {
                float dstToPlayer = Vector3.Distance (transform.position, player.position);
                if (!physics.Raycast(transform.position, dirToPlayer, dstToPlayer, obstacleMask))
                {
                    m_PlayerInRange = true;
                    m_IsPatrol = false; 
                }
                else
                {
                    m_PlayerInRange = false;
                }
            }
            if (Vector3.Distance(transform.position, player.position)> viewRadius)
            {
                m_PlayerInRange = false;
            }
        }
        if (m_PlayerInRange)
        {
            m_PlayerPosition = player.transform.position;
        }
    }




}

Okay so here is the list of issues I found in that:

  • edgeInteractions was a int even thought the value you have there is a float
  • on line 54 you have the navmesh.setdestination input as a transform rather than a Vector3, so ive just added .position to fix that
  • Line 96 you misspelled .reminaingDistance and stoppingDistance
  • Line 98 it looks like you’re trying to get the distance from the transform to the players transform, its better not to use .FindGameObjectWithTag and use a reference to the player instead for performance, so Ive added a variable player and fixed line 98 as brackets were incorrectly used
  • Line 125 you misspelled the function LookingPlayer
  • Line 130 im assuming your trying to use Time.deltaTime
  • Line 138 youve misspelled remainingDistanec and stoppingDistance
  • Line 149 assuming its supposed to be Time.deltaTime
  • Line 155 youve misspelled your function
  • Line 176 assuming Its supposed to be Time.deltaTime
  • Line 182 and 190 formatting issues like incorrect uses of brackets or misspellings of functions or class like Physics

I recommend watching some youtube videos on the basics of c# and unity as most of these issues were syntax issues, misspellings, missing caps etc

Here is the script without errors, I have not checked the script does what you want though I just fixed the errors, for future cases double check all your spellings and make sure to put Caps in the correct places as C# is case sensitive (Dont forget to reference the player in the inspector as I added it as a variable):

using UnityEngine;
using UnityEngine.AI;

public class EnemyAI2 : MonoBehaviour
{
    public NavMeshAgent navMeshAgent;
    public float startWaitTime = 4;
    public float timeToRotate = 2;
    public float speedWalk = 2;
    public float speedRun = 9;

    public float viewRadius = 15;
    public float viewAngle = 90;
    public LayerMask playerMask;
    public LayerMask obstacleMask;
    public float meshResolotion = 1f;
    public float edgeInteractions = 0.4f;
    public float edgeDistance = 0.5f;
    public Transform player;

    public Transform[] waypoints;
    int m_CurrentWaypointIndex;

    Vector3 playerLastPosition = Vector3.zero;
    Vector3 m_PlayerPosition;

    float m_WaitTime;
    float m_TimeToRotate;
    bool m_PlayerInRange;
    bool m_PlayerNear;
    bool m_IsPatrol;
    bool m_CaughtPlayer;

    // Start is called before the first frame update
    void Start()
    {
        m_PlayerPosition = Vector3.zero;
        m_IsPatrol = true;
        m_CaughtPlayer = false;
        m_PlayerInRange = false;
        m_WaitTime = startWaitTime;
        m_TimeToRotate = timeToRotate;

        m_CurrentWaypointIndex = 0;
        navMeshAgent = GetComponent<NavMeshAgent>();

        navMeshAgent.isStopped = false;
        navMeshAgent.speed = speedWalk;
        navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);

    }

    // Update is called once per frame
    void Update()
    {
        EnviromentView();
        if (!m_IsPatrol)
        {
            Chasing();
        }
        else
        {
            patroling();
        }
    }

    private void Move(float speed)
    {
        navMeshAgent.isStopped = false;
        navMeshAgent.speed = speed;
    }

    void CaughtPlayer()
    {
        m_CaughtPlayer = true;
    }

    void Stop()
    {
        navMeshAgent.isStopped = true;
        navMeshAgent.speed = 0;
    }

    private void Chasing()
    {
        m_PlayerNear = false;
        playerLastPosition = Vector3.zero;

        if (!m_CaughtPlayer)
        {
            Move(speedRun);
            navMeshAgent.SetDestination(m_PlayerPosition);
        }
        if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
        {
            if (m_WaitTime <= 0 && !m_CaughtPlayer && Vector3.Distance(transform.position, player.transform.position) >= 6f)
            {
                m_IsPatrol = true;
                m_PlayerNear = false;
                Move(speedWalk);
                m_TimeToRotate = timeToRotate;
                m_WaitTime = startWaitTime;
                navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
            }
            else
            {
                if (Vector3.Distance(transform.position, GameObject.FindGameObjectWithTag("Player").transform.position) >= 2.5f)
                {
                    Stop();
                    m_WaitTime -= Time.deltaTime;
                }
            }
        }
    }

    private void patroling()
    {
        if (m_PlayerNear)
        {
            if (m_TimeToRotate <= 0)
            {
                Move(speedWalk);
                LookingPlayer(playerLastPosition);
            }
            else
            {
                Stop();
                m_TimeToRotate -= Time.deltaTime;
            }
        }
        else
        {
            m_PlayerNear = false;
            playerLastPosition = Vector3.zero;
            navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
            if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
            {
                if (m_WaitTime <= 0)
                {
                    NextPoint();
                    Move(speedWalk);
                    m_WaitTime = startWaitTime;
                }
                else
                {
                    Stop();
                    m_WaitTime -= Time.deltaTime;
                }
            }
        }
    }

    public void NextPoint()
    {
        m_CurrentWaypointIndex = (m_CurrentWaypointIndex + 1) % waypoints.Length;
        navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
    }

    void LookingPlayer(Vector3 player)
    {
        navMeshAgent.SetDestination(player);
        if (Vector3.Distance(transform.position, player) <= 0.3)
        {
            if (m_WaitTime <= 0)
            {
                m_PlayerNear = false;
                Move(speedWalk);
                navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);
                m_WaitTime = startWaitTime;
                m_TimeToRotate = timeToRotate;
            }
            else
            {
                Stop();
                m_WaitTime -= Time.deltaTime;
            }
        }
    }

    void EnviromentView()
    {
        Collider[] playerInRange = Physics.OverlapSphere(transform.position, viewRadius, playerMask);
        for (int i = 0; i < playerInRange.Length; i++)
        {
            Transform player = playerInRange[i].transform;
            Vector3 dirToPlayer = (player.position - transform.position).normalized;
            if (Vector3.Angle(transform.forward, dirToPlayer) < viewAngle / 2)
            {
                float dstToPlayer = Vector3.Distance(transform.position, player.position);
                if (!Physics.Raycast(transform.position, dirToPlayer, dstToPlayer, obstacleMask))
                {
                    m_PlayerInRange = true;
                    m_IsPatrol = false;
                }
                else
                {
                    m_PlayerInRange = false;
                }
            }
            if (Vector3.Distance(transform.position, player.position) > viewRadius)
            {
                m_PlayerInRange = false;
            }
        }
        if (m_PlayerInRange)
        {
            m_PlayerPosition = player.transform.position;
        }
    }
}