Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

hello every, please help me

Discussion in 'Scripting' started by chen821114, Nov 10, 2018.

  1. chen821114

    chen821114

    Joined:
    Nov 9, 2017
    Posts:
    14
    hello every, my english is not good, sorry.


    public class EnemyMovement : MonoBehaviour
    {
    Transform player; // Reference to the player's position.
    PlayerHealth playerHealth; // Reference to the player's health.
    EnemyHealth enemyHealth; // Reference to this enemy's health.
    NavMeshAgent nav; // Reference to the nav mesh agent.

    void Awake ()
    {
    // Set up the references.
    player = GameObject.FindGameObjectWithTag ("Player").transform;
    playerHealth = player.GetComponent <PlayerHealth> ();
    enemyHealth = GetComponent <EnemyHealth> ();
    nav = GetComponent <NavMeshAgent> ();
    }


    i want to ask about what is the function of line3 to line6

    "" Transform player; // Reference to the player's position.
    PlayerHealth playerHealth; // Reference to the player's health.
    EnemyHealth enemyHealth; // Reference to this enemy's health.
    NavMeshAgent nav; // Reference to the nav mesh agent.""
    i don't know it's function and meanimg, please tell me. thank you very much
     
    Last edited: Nov 10, 2018
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,037
    Please always put your example code in code tags in the forums.

    To answer your question:
    You're declaring class fields in these lines which you can understand as instantiated object-wide usable variables (if we want to make it super-simple).
    Then in the Awake method you add value to these "variables", basically look for some objects in the scene and store the reference to them in these.
    After the Awake method, you will be able to reach those objects and call their methods or use their public variables (fields) to calculate data or modify the state of your application.

    Let me know if it's clear enough. Also you can read up about these here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields
     
  3. chen821114

    chen821114

    Joined:
    Nov 9, 2017
    Posts:
    14
    thank you very much