Search Unity

Enemy clones attack

Discussion in 'Scripting' started by GS.Saturos, Jul 31, 2014.

  1. GS.Saturos

    GS.Saturos

    Joined:
    Jul 2, 2014
    Posts:
    17
    I currently have an enemy with a script attached to it so it interacts with the player. Today, I also added a simple spawn system to get started with spawning enemies in the scene, but the enemy clones my script spawns do not interact at all like the original enemy object does.

    How can I make the clones behave the same way as its original object does? The scripts, audio, animation, meshes, etc. all transferred to the clones when they spawn, but the clones just stand idle as if no scripts were added to it. An example being my original enemy object chases my player when it gets close, but the clones don't.
     
  2. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    If you could paste the scripts you're working with then we'd have a better idea of how to help you.
     
  3. GS.Saturos

    GS.Saturos

    Joined:
    Jul 2, 2014
    Posts:
    17
    The GameObject is the enemy prefab that I have. The original enemy prefab chases the player with a script I gave it, but its clones don't when they spawn from the script below.

    public float counter;

    public GameObject enemy;

    // Use this for initialization
    void Start ()
    {
    StartCoroutine(EnemySpawn());
    }

    IEnumerator EnemySpawn()
    {
    enemy = GameObject.FindWithTag("Enemy");

    for(counter = 1; counter < 6; counter++)
    {
    Instantiate(enemy, transform.position, Quaternion.identity);
    yield return new WaitForSeconds(1.5f);

    if(counter >= 6)
    break;
    }

    }
     
  4. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Can you paste the original "chase" script as well?

    Also, there's no reason to check if counter >= 6 to break the loop if the highest index of the for-loop is 6.
     
  5. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
  6. GS.Saturos

    GS.Saturos

    Joined:
    Jul 2, 2014
    Posts:
    17
    @ FlightCrazed Thanks for the link to that tutorial. Helped a lot more.
    @ mafiadude1 Thanks for the help as well. I set counter >= 6 to break just to be sure in case it glitches and jumps past 6 for whatever reason, but I'll set it to check at 6 and see what happens.
     
  7. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    If you want to make sure it doesn't go past 6 then you should probably put that if block before the instantiate :p