Search Unity

Enemy gets told it's target but it disappears completely

Discussion in 'Scripting' started by ChironeV, Jul 26, 2014.

  1. ChironeV

    ChironeV

    Joined:
    Jul 26, 2014
    Posts:
    9
    Hi all,

    I'm working in C#
    I have an enemy game object with a script attached. This script has a public GameObject variable called target.
    The enemy game object is meant to go towards the target in a linear fashion.
    The target gets spawned, the enemy gets its target set to the newly spawned target but it doesn't go anywhere.
    In fact the target it's tracking is null.

    I have a script called SceneController. in this script there is a spawn method like so:
    Code (CSharp):
    1. void DoSpawnPlayer()
    2.     {
    3.         GameObject playerObject = playerSpawner.DoSpawnPlayer();
    4.  
    5.         foreach(GameObject threatObject in threatObjects)
    6.         {
    7.             Threat threat = threat.GetComponent<Threat>();
    8.             threat.target = playerObject ;
    9.         }
    10.     }
    In the threat I have a method called in Update called MoveTowardsTarget
    Code (CSharp):
    1. public void MoveTowardsTarget()
    2.     {
    3.         Debug.Log("target: " + target + " self position: " + transform.position);
    4.         MoveTowardsTarget(target);
    5.     }
    Now... the target variable in the Enemy script is always null, but in the SceneController it reports it back as being not null.
    I've checked to see if there is another instance of the Enemy in the game and there appears to only be ONE.
    I've printed the position of the Enemy in both the Enemy script and the SceneController script and they are completely different.

    I don't understand what's wrong.
    The Enemy and Spawn location were dropped in as a prefab but the player is instantiated.

    I hope I'm clear with my explanation
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    This line right here:

    The enemy game object is meant to go towards the target in a linear fashion.

    Are you trying to calculate the direction of travel? Right when the gameObject spawns, it will move towards it? In your move towards function you need to calculate the direction and distance of travel.

    To do this, use the formula: direction = destination - source.

    That will calculate the distance to travel as well as return a resultant vector which is the direction of travel. To make it move, you'll need to pass it through a character controller move function, and or transform.Translate or some other method of your choice.

    If this isn't what you're trying to do, please explain a bit more.


    Best,

    Jon
     
  3. ChironeV

    ChironeV

    Joined:
    Jul 26, 2014
    Posts:
    9
    I had this working once before but the target the enemy would go after would only be the one that was already dropped into the scene. So I had to get some other script to know when a new player has spawned.

    So I know that the enemy can chase but for whatever reason it think it's got no target even though it gets given one.