Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pausing animation/movement of a Clone (instantiated enemy)

Discussion in 'Scripting' started by jococo, Aug 26, 2014.

  1. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    I'm trying to implement a simple pause button and have been able to pause all animations and motions of my various characters in the game except the clones.

    I instantiate enemies a few seconds after loading level and they produce clones with names like "Orc_Mage(Clone)", etc.

    To find the game objects to "pause" I do this:

    Code (JavaScript):
    1. orc_pikeman-clone = GameObject.Find("orc_pikeman(Clone)").transform;
    Then I try to pause it's animations with this:

    Code (JavaScript):
    1. orc_pikeman_Clone.SendMessage("buttonPauseHandler", 0);
    This technique works fine for all enemies except the ones that are instantiated and produce a clone.

    Do I have to do something special to pause the animation/movements of an instantiated clone?

    Thanks!
     
  2. ahaykal

    ahaykal

    Joined:
    Apr 18, 2010
    Posts:
    117
    I am not sure if this is the problem : but when you are trying to find the object the variable name is :"orc_pikeman-clone" when you are trying to access it the name is : orc_pikeman_Clone.
     
  3. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    Thanks ahaykai!

    Good catch but this code I posted got accidently changed by me.

    I have this variable defined in the script:

    Code (JavaScript):
    1. private var orc_pikeman_Clone : Transform;
    That typo isn't in my code so that isn't the problem I don't think.

    Code (JavaScript):
    1. orc_pikeman_Clone = GameObject.Find("orc_pikeman(Clone)").transform;

    Code (JavaScript):
    1.     orc_pikeman_Clone.SendMessage("buttonPauseHandler", 0);
    2.  

    I should probably add that buttonPauseHandler does this:

    Code (JavaScript):
    1. function buttonPauseHandler()
    2. {
    3. Debug.Log( "buttonPauseHandler - EnemyOcrPikeman");
    4.     animation.enabled=false;
    5.  
    6.     movementBeforePause.x = movement.x;
    7.     movementBeforePause.y = movement.y;
    8.     movementBeforePause.z = movement.z;
    9.  
    10.  
    11.     movement.x = 0.0f;
    12.     movement.y = 0.0f;
    13.     movement.z = 0.0f;
    14.  
    15. }
     
    Last edited: Aug 26, 2014
  4. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    No one has ever encountered this issue?! Seriously?
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Do you have multiple enemies with the same name ( i.e multiple enemy_name(clone) ) ?
     
  6. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    Yes I spawn clones throught the game. Is that bad?
     
  7. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Code (csharp):
    1. orc_pikeman_Clone = GameObject.Find("orc_pikeman(Clone)").transform;
    would only find one of the clones, you could use tags, and tag all enemies as "enemy" and use GameObject.FindObjectsWithTag or you could change the .name of each clone so it's GameObject.Find("orc_pikeman_" + count); and go through all enemies
     
  8. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    Thanks image28!

    So I changed it around and can find all GameObjects with a tag of "enemies"

    Code (JavaScript):
    1. var allEnemiesGO = GameObject.FindGameObjectsWithTag("Enemies");
    Not sure how to pause a GameObject's animation though.

    I tried

    Code (JavaScript):
    1. allEnemiesGO.animation.enable = false;
    " 'animation' is not a member of 'UnityEngine.GameObject[]'."
     
    Last edited: Aug 27, 2014
  9. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    FindGameObjectsWithTag returns an array of gameobjects.... don't use javascript but in csharp we would do something like this.... ( haven't tested this code and it's in C# )

    Code (csharp):
    1.  
    2. GameObject[] allEnemiesGO = GameObject.FindGameObjectsWithTag("Enemies");
    3.  
    4. foreach( GameObject enemy in allEnemiesGO )
    5. {
    6. enemy.animation.enable=false;
    7. }
    8.  
     
    jococo likes this.
  10. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Just tested that code... works properly.... again C#
     
  11. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    Nice! That makes sense now. Thanks image28!

    Yes, I prefer c# but having to use js currently. I'll try to convert it ...