My animation does not return to idle

I have a spawn script which does work:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;




public class SpawnManager : MonoBehaviour

{

    GameObject myCharacter = null;

    GameObject myCharacterSpawnPoint = null;

    Transform myCharacterTransform;




    // Use this for initialization

    void Start()

    {

        // spawn the character

        myCharacterTransform.position = Vector3(0.0f, 0.0f, 0.0f);

        for (int i = 0; i < 2; i++)

        {

            // add a little randomness to spawning

        Vector3 tempPos = myCharacterSpawnPoint.transform.position;

            tempPos.x += Random.Range(2, 4);

            tempPos.z += Random.Range(2, 4);

            myCharacterSpawnPoint.transform.position = tempPos;




            myCharacter = Instantiate(Resources.Load("Characters\\MyCharacter"),               grannyTransform.position, Quaternion.identity) as GameObject;

            myCharacter .name = "MyCharacter" + i;

        }

    }

}

I also have a GotoWayPoint script that is attached to my character in resources:
If I have one character spawn in the for loop everything works.
If I have more that one character spawn in the for loop only the last character returns to the idle position.
All characters stop moving (but keeps animating except for the one) which is normal when they reach the waypoint.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;




public class GotoWayPoint : MonoBehaviour

{

    private Transform targetWayPoint.position = Vector3(10.0f, 0.0f, 10.0f);




    public bool isIdle;

    public bool isWalking;




    public float positionSpeed = 10;

    public float rotationSpeed = 2;




    // control the animation

    static Animator animator;




    private Transform characterTransform;


    // Use this for initialization

    void Start()

    {

        // get a reference to the animator

        animator = GetComponent<Animator>();




        animator.SetBool("isWalking", isWalking);

        animator.SetBool("isIdle", isIdle);

    }







    // Update is called once per frame

    void Update()

    {

        // rotate towards the target

        Vector3 targetDir = (targetWayPoint.position - transform.position).normalized;

        Quaternion lookRotation = Quaternion.LookRotation(targetDir) * Quaternion.Euler(0, rotationOffset, 0);

        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);




        // move towards the target

        transform.position = Vector3.MoveTowards(transform.position, targetWayPoint.position, positionSpeed * Time.deltaTime);




        if (Vector3.Distance(transform.position, targetWayPoint.position) < 1)

        {

            Debug.Log(this.name + " Stop*******************************");

        

            // reached the waypoint

            animator.SetBool("isWalking", false);

            animator.SetBool("isIdle", true);

        }

    }

}

When posting code, please use the code tags to properly format it. See this post for details on how:
Using Code Tags Properly

To the issue with your code, you have the field that is storing the reference to the Animator set as static

static Animator animator;

The static keyword means that there can only be one instance of this field across your entire application. This means that your calls to the Animator reference for things like

animator.SetBool("idle", isIdle");

Will result in the message only being sent to a single Animator in your application, rather than each of them as necessary.
You should change that static reference to a private reference instead.

private Animator animator;

This will mean that each character will be sending messages to the controller that is attached to it and they can all act independent of each other.

Cheers,

TrickyHandz

The quotes was pasted poorly into this message,
The static advise was bang on, it does make sense to me now.
Thank you very much for the help. My characters stop when they are told.

1 Like