Search Unity

Question multiple errors in line 105

Discussion in 'Scripting' started by egishlol, Jan 27, 2023.

  1. egishlol

    egishlol

    Joined:
    Jan 27, 2023
    Posts:
    1
    I have the following errors in line (105,5):
    Assets\EnemyAttacking.cs(105,5): error CS1003: Syntax error, '(' expected
    Assets\EnemyAttacking.cs(105,5): error CS1525: Invalid expression term '}'
    Assets\EnemyAttacking.cs(105,5): error CS1002: ; expected
    Assets\EnemyAttacking.cs(105,5): error CS1002: ; expected

    I tried solving them myself but cant seem to find the issue

    here is the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3.  
    4. public class EnemyAi : MonoBehavior
    5. {
    6.     public NavMeshAgent agent;
    7.  
    8.     public transform player;
    9.  
    10.     public LayerMask WhatIsGround, WhatIsPlayer;
    11.  
    12.     public float health;
    13.  
    14.     //Patroling
    15.     public Vector3 walkPoint;
    16.     bool walkPointSet;
    17.     public florat walkPointRange;
    18.  
    19.     //attacking
    20.     public float TimeBetweenAttacks;
    21.     bool alreadyAttacked;
    22.     public GameObject projectile;
    23.  
    24.     //states
    25.     public float sightRange, attackRange;
    26.     public bool playerInSightRange, playerInAttackRange;
    27.  
    28.     private void Awake()
    29.     {
    30.         player = GameObject.find("PlayerObj").transform;
    31.         agent = GetComponent<NavMeshAgent>();
    32.  
    33.     }
    34.    
    35.     private void update()
    36.     {
    37.         playerInSightRange = Physics.CheckSphere(transform.position, sightRange, WhatIsPlayer);
    38.         playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, WhatIsPlayer);
    39.  
    40.         if (!playerInSightRange && !playerInAttackRange) Patroling();
    41.         if (playerInSightRange && !playerInAttackRange) ChasePlayer();
    42.         if (playerInAttackRange && playerInSightRange) AttackPlayer ();
    43.  
    44.     }
    45.     private void Patroling()
    46.     {
    47.         if (!walkPointSet) SearchWalkPoint();
    48.  
    49.         if (walkPointSet)
    50.             agent.SetDestination(walkPoint);
    51.  
    52.         Vector3 distanceToWWalkPoint = transform.position - walkPoint;
    53.  
    54.         //walkpoint reached
    55.         if (distanceToWWalkPoint.magnitude < 1f)
    56.          walkPointSet = false;
    57.     }
    58.  
    59.     private void SearchWalkPoint()
    60.     {
    61.         //as randomx and randomz insert minimum and max z and x values to which entities should walk to,
    62.         float randomZ = Random.Range(-walkPointRange, walkPointRange);
    63.         float randomX = Random.Range(-walkPointRange, walkPointRange);
    64.  
    65.         walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
    66.  
    67.         if (Physics.Raycast(walkPoint, -transform, 2f, WhatIsGround))
    68.             walkPointSet = true;
    69.     }
    70.  
    71.     private void ChasePlayer()
    72.     {
    73.         agent.SetDestination(player.position);
    74.     }
    75.  
    76.     private void AttackPlayer()
    77.     {
    78.         //making sure enemy doesnt move
    79.         agent.SetDestination(transform.position);
    80.  
    81.         transform.LookAt(player);
    82.  
    83.         if (!alreadyAttacked)
    84.         {
    85.             //attack code here
    86.             Rigidbody rb = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Rigidbody>();
    87.             rb.AddForce(transform.forward * 32f, ForceMode.Impulse);
    88.             rb.AddForce(transform.up * 8f, ForceMode.Impulse);
    89.  
    90.  
    91.            
    92.             alreadyAttacked = true;
    93.             Invoke(nameof(ResetAttack), TimeBetweenAttacks);
    94.         }
    95.     }
    96.     private void ResetAttack ()
    97.     {
    98.         alreadyAttacked = false;
    99.     }
    100.  
    101.     public void TakeDamage(int damage)
    102.     {
    103.         health -= damage;
    104.  
    105.         if (health <= 0) Invoke(nameof(DestroyEnemy), .5f);
    106.     }
    107.  
    108.     private void DestroyEnemy()
    109.     {
    110.         Destroy(GameObject);
    111.     }
    112.  
    113.  
    114.  
    115. }
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    You posted the
    EnemyAi
    script but the error is in
    EnemyAttacking
    .
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    As Adrian points out, you need to look where the error actually is.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!) <--- get this correct!!!
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    Also, this might save you a lot of time if you plan on monkey-hammer-banging code in from tutorials / examples:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! See the top of this post.
     
  4. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    As Adrian has pointed out, we would need you to post the correct script (EnemyAttacking) to help you out with that.

    However, as we are looking at EnemyAi you should note that you have a mistake on line 35:
    private void update()

    C# is a case-sensitive language, so this method should be:
    private void Update()
     
    Kurt-Dekker likes this.