0
\$\begingroup\$

In a small game that I'm making enemies multiply when you kill them, however when clones are instantiated they don't chase the player like the original prefabs do (example: http://gfycat.com/VastActualElver)

Here is relevant part of my code:

 #pragma strict
    var moveSpeed : int = 3;

    var player : Transform;
    var MaxDist = 10;
    var MinDist = 1;

    function Update () {
        if(curhealth <= 0){
            Dead();
        }
        transform.LookAt(player); 
        if(Vector3.Distance(transform.position, player.position) >= MinDist){ 
            transform.position += transform.forward * moveSpeed * Time.deltaTime;
        }
    }

function Dead () {
    Destroy(gameObject);
    var posx = Random.Range(-10, 10);
    var posz = Random.Range(-10, 10);
    Instantiate(enemys, Vector3(posx, 0.5, posz), transform.rotation);
}

What am I missing?

\$\endgroup\$
3
\$\begingroup\$

You are not setting the Player transform of your clones, most likely.

If I had to guess, you drag and drop the Player object onto your prefabs that are in your scene. This effectively maps your player onto your Transform object. However, if you clone the enemy, the object is a new instance, and the player transform is not initialized.

Example on how to set player transform in code:

player = GameObject.Find("PlayerObject").transform;
\$\endgroup\$
3
  • \$\begingroup\$ That is what I did. Would I have to assign the variable through code or is there another way? \$\endgroup\$ – eren_tetik Oct 7 '14 at 5:47
  • \$\begingroup\$ As far as I know, you will need to assign the Transform in code. You might be able to do some sort of deep clone, but I don't know how to do this. I'll update the answer to show an example. \$\endgroup\$ – jgallant Oct 7 '14 at 10:20
  • \$\begingroup\$ Changed it to: player = GameObject.FindWithTag("Player").transform; Worked fine, thanks! \$\endgroup\$ – eren_tetik Oct 10 '14 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.