Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

hello every, please help me

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

  1. chen821114

    chen821114

    Joined:
    Nov 9, 2017
    Posts:
    12
    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. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,189
    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:
    12
    thank you very much