Search Unity

Animation.enabled = false; only working sometimes?

Discussion in 'Scripting' started by twomandeep, Mar 15, 2018.

  1. twomandeep

    twomandeep

    Joined:
    Dec 24, 2017
    Posts:
    5
    Hi guys,

    I have a simple game in which you throw axes at targets, with a slider determining when the axe flies forwards.

    In my script, I have a custom function that plays a sound, stops the slider animations and instantiates the axe. About 80% of the time this works fine, but occasionally, the animations will continue to play. I'd have thought you'd get the same result every time a function is called, as it's just repeating the same process. Or does this bug have to be coming from somewhere else in the script?

    Here's the Function:

        public void SpawnAxe(){

    axeThrow.Play (); //play axe sound.
    axeReady = false;
    spawnerAnimator.enabled = false; //freezes spawner animation.
    UISliderAnimator.enabled = false; //freezes UI slider animation.
    Instantiate (axe, axeSpawn); //spawns axe.

    }


    This is the if function that calls it:

            if (Input.GetMouseButtonDown (0) && !GameOver && !inTransit) { //allows axe to be thrown on click/screen touch.

    if (axeReady) {

    SpawnAxe ();

    }

    }


    Been trying to sort this for a while, can't understand why a function would fire differently under the same parameters and only work some of the time?

    Thanks in advance!

    TMD.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It might be the animation is picking up where it left off instead of starting over. Are you using Mecanim? Because it would be better to control the animations with a bool or something. If not, by using stop and start.
     
  3. twomandeep

    twomandeep

    Joined:
    Dec 24, 2017
    Posts:
    5
    Yes, the animation does pick up where it left off, which is what I want really. But it seems to pause and unpause steamlessely and works exactly how i'd like 80% of the time. It's just every now and again it doesn't.
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If you start at the end of the animation, does it restart again right away?
     
  5. twomandeep

    twomandeep

    Joined:
    Dec 24, 2017
    Posts:
    5
    Yeah, it loops fine. But it enables and disables at several points due to menu's etc and they all work fine apart from this one function which sometimes messes up. It's really weird. The only thing I can think is another call for it to be enabled is sometimes calling because of something else firing off? But I can't find any errors or evidence from debugging to support that.
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    There's an animation.isPlaying function or variable or something. Myself, though, I would use mecanim for animations. Disabling animators seems a little overkill. I'm working on a game now where I skipped using a state machine and there is irratic behaviour because it's too many balls in the air. I'm using mecanim, but for the ai states, I should have used a state machine. It's been a nightmare debugging it and some things I just have to leave because I cannot find them.
     
  7. twomandeep

    twomandeep

    Joined:
    Dec 24, 2017
    Posts:
    5

    Cool man, I appreciate the feedback. I've got a couple other things to try for debugging but I'll definitely look into isPlaying and mecanim for sure.

    Thanks! :)

    Any other ideas or solutions, please feel free to post as well. I'll update if solution is found :)
     
  8. twomandeep

    twomandeep

    Joined:
    Dec 24, 2017
    Posts:
    5
    UPDATE: I've got the bug fixed. I've been using onCollisionEnter(other) for all of my collision. But I wasn't checking within that if the thing collider was an Axe or not. I did this through tags. If (other.gameObject.CompareTag("Axe")). Now, the animations work fine. It must have been snaggin on something in the scene and firing at random points through the map.

    Definitely advise checking on any Collision through tags, even if it's just for debugging purposes!