Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question I need help

Discussion in 'Scripting' started by SUOS2007, Jun 25, 2020.

  1. SUOS2007

    SUOS2007

    Joined:
    Jun 5, 2020
    Posts:
    3
    I need help, i'm fairly new to Unity, and i'm only 13, it's a bit complicated. I watched a tutorial on youtube on a csharp script to make zombies attack my player, my script matches the tutorial but in unity, it shows "Assets\Scripts\Zombie.cs(26,42): error CS1955: Non-invocable member 'Animation' cannot be used like a method." and "Assets\Scripts\Zombie.cs(26,21): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement"
    Here's my code, can anyone help?


    Code (CSharp):
    1. public class Zombie : MonoBehaviour
    2. {
    3.     public GameObject Player;
    4.     public float TargetDistance;
    5.     public float AllowedRange = 10;
    6.     public GameObject enemy;
    7.     public float EnemySpeed;
    8.     public int AttackTrigger;
    9.     public RaycastHit shot;
    10.  
    11.     void Update()
    12.     {
    13.         transform.LookAt(Player.transform);
    14.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out shot))
    15.         {
    16.             TargetDistance = shot.distance;
    17.             if (TargetDistance < AllowedRange)
    18.             {
    19.                 EnemySpeed = 0.02f;
    20.                 if (AttackTrigger == 0)
    21.                 {
    22.                     enemy.GetComponent < Animation().Play("Z_Walk");
    23.                     transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, EnemySpeed);
    24.                 }
    25.             }
    26.  
    27.             else
    28.             {
    29.                 EnemySpeed = 0f;
    30.                 enemy.GetComponent < Animation().Play("Z_Idle");
    31.             }
    32.         }
    33.     }
    34. }
    35.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Hey i think you're just missing a '>' on line 22 (though your error message says line 26. Maybe you changed the code since then?)

    Code (CSharp):
    1. enemy.GetComponent<Animation>().Play("Z_Walk");
    Similarly on line 30:
    Code (CSharp):
    1. enemy.GetComponent<Animation>().Play("Z_Idle");
    I love that you're only 13 and trying to tackle game dev. Keep it up!
     
    Kurt-Dekker and SUOS2007 like this.
  3. SUOS2007

    SUOS2007

    Joined:
    Jun 5, 2020
    Posts:
    3
    It worked! Thank you so much! I was really confused at first, and now i'm a bit embarassed for forgetting something as simple as that.
     
    PraetorBlue likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Those angle brackets (less than greater than) that @praetor pointed out can be confusing because even in the few lines of code above, you can see they're used in more than one way:

    First line 17 uses them to ask "is target distance LESS THAN allowed range?"

    And line 22 and 30 (the ones where you had the greater-than missing) means something completely different. Used in that context it means "use the flavor of
    GetComponent
    (which is a function) and make sure it gives me a component that is of
    Animator
    type". The official term for that is a "generic" or "templated" function.

    That kind of dual meaning happens in a few different places in C# but soon you get so you just sorta read past it.

    There are other symbols like that too. For instance dot (.) can mean decimal point, or else it can mean look up a member of a class.