Search Unity

[SOLVED] transform.position not updating

Discussion in 'Scripting' started by instantaphex, Dec 2, 2015.

  1. instantaphex

    instantaphex

    Joined:
    Nov 29, 2015
    Posts:
    1
    I'm going through the survival shooter tutorial and I'm having an issue when using a nav mesh agent as described in the tutorial.

    The tutorial provides the following code as well as video instruction on baking a nav mesh and creating a nav mesh agent. It seems to be working fine, I have an "enemy" character set up as a nav mesh agent as well as a nav mesh. The enemy is set up to follow the player using the following code:

    usingUnityEngine;
    usingSystem.Collections;

    publicclassEnemyMovement : MonoBehaviour
    {
    Transformplayer;
    //PlayerHealthplayerHealth;
    //EnemyHealthenemyHealth;
    NavMeshAgentnav;


    voidAwake ()
    {
    player = GameObject.FindGameObjectWithTag ("Player").transform;
    //playerHealth = player.GetComponent <PlayerHealth> ();
    //enemyHealth = GetComponent <EnemyHealth> ();
    nav = GetComponent <NavMeshAgent> ();
    }


    voidUpdate ()
    {
    //if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
    //{
    nav.SetDestination (player.position);
    //}
    //else
    //{
    //nav.enabled = false;
    //}
    }
    }

    The result is that the enemy character follows a path towards (0.0, 0.0, 0.0) and just stays there when it gets there. I added a Debug.log(player.position) and the log is just showing the same x, y and z coordinates as I move the player around the screen. I'm new to Unity so please let me know if there is anything important that I'm leaving out.

    UPDATE:

    Using a clue from another similar post, I added the following code to my EnemyMove code:

    GameObject[] playerObjects = GameObject.FindGameObjectsWithTag("Player");
    foreach(GameObjectplayerObjectinplayerObjects)
    {
    Debug.Log("Found object: " + playerObject);
    }

    Then I removed the Player tag from the player and I was still getting one line of output from the console. Now that I may have found the problem, how do I find the mistakenly tagged game object? I've gone through everything in the hierarchy with no luck. Is there another way to find it?

    UPDATE 2:

    It looks like this is a known bug in Unity. For some reason Unity had created a clone of the Player GameObject and FindGameObjectsWithTag() was returning the clone as well as the original. The solution was to close and reopen Unity.

    Thanks
     
    Last edited: Dec 2, 2015