# Changing animator via scriping

I am using the following code, my intention is whenever the 2 objects meet the animator boolean changes and when leave change back to its original setting. But it doesnt seem to work.

  public GameObject ThePlayer;
public float TargetDistance;
public float EnemyHealth;
public float AllowedRange = 10;
public GameObject TheEnemy;
public float EnemySpeed;
public Animator animator;
public int AttackTrigger;
public RaycastHit Shot;
public static bool hasAttacked = false;
private bool gapTest = true;

void Start()
{

animator = GetComponent<Animator>();
hasAttacked = false;
}

void OnCollisionEnter(Collision col)
{

if (col.gameObject.name == "FPSController")
{
Debug.Log("hit");
animator.SetBool("Moving", false);
animator.SetBool("Attack1Trigger", true);
hasAttacked = true;
}

}

void OnTriggerExit(Collider col)
{
if (col.gameObject.name == "FPSController")
{
Debug.Log("left");
animator.SetBool("Moving", true);
animator.SetBool("Attack1Trigger", false);
hasAttacked = true;
}
}


Any tips on why this does not work?

This is hitting the debug.log in the console, but it is not hitting the animator changes or the boolean change

• Three tips: 1) I note you're using collision for entrance but trigger for exit — is this deliberate? 2) I generally recommend against matching game objects by name — are you sure you're hitting the exact named object you think, and not a clone / child of it that has a slightly different name? 3) If neither of these areas of investigation help, can you edit your question to include your debugging findings so far? Are the enter/exit methods getting called at all? Do the if tests pass? Can you see the booleans changing in-editor? – DMGregory Mar 15 at 14:03
• when i have it as 'OnCollisionEnter' it is hitting the debug.log line but not the animators – Gurdeep Mar 15 at 14:22
• The runtime doesn't skip selected lines in the same block. If the Debug.Log line gets hit, then the following lines will be executed too. Are you seeing a runtime error like a null reference exception that could cause execution to terminate in the middle of a block? If not, you may be setting the bools, but not on the instance you thought you were, or not using the results of that setting, or setting them back to their old values immediately in a separate method call in the same frame. – DMGregory Mar 15 at 15:01

Classic case of silly mistake. I think you need to be using OnTriggerEnter but you're using OnCollisionEnter. :) Good luck friend

• Thanks for your reply! I have tried the way you said and unfortunately no luck! when i have it as 'OnCollisionEnter' it is hitting the debug.log line but not the animators – Gurdeep Mar 15 at 14:23
• Both debug.logs are getting hit? Then the call to the animator is DEFINITELY going through. So the way I see it, one of two things could be going wrong. Either the string is misspelled (it is case sensitive so make sure it's EXACTLY as defined in your animator), OR it's getting the wrong animator. To verify it's the correct animator, click on the animation controller linked in your animator so it's selected, then have the animation tab open. When you click play, the window should update with a live preview. You can verify the bool is being set. You'll see moving lines in the view. – gjh33 Mar 15 at 18:27
• Also possible there's a null reference error you're not seeing and the animator isn't being set at all (i.e. GetComponent returns null). – gjh33 Mar 15 at 18:27

I've been working with the animator a fair amount recently so here's a few things you can try in situations like these:

Verify you're hitting an/the right animator The first part of this should be relatively easy. If the animator is null you should be seeing console errors. If not though, do a quick check in the editor to make sure whatever animator is hooked up to the public field is in fact the one you're looking at. Sounds silly, but I've had that problem before.

Check the state of the variable in code You can do this all the time or just immediately after set, but all the Set methods have corresponding Get methods, so you should be able to call that to see what the state is. This is helpful to see if something else may just be turning it off very quickly, at which case you can search for that instead.

Look for typos This is another one that has bitten me before. Since both the parameter in the animator and the one you're passing into that method are typed in manually with no verification, it's possible to make a typo on one of the two such that they don't match and the animator will just silently accept and ignore you when you try to Set.

Hopefully one of these will help you find what is going wrong in your case :)

• To add to typos. I always make constants for the string names of the variables somewhere in my code to prevent them. It also means if you change the variable name you only have to change one location in the code. – gjh33 Mar 18 at 13:22
• Definitely a good practice, unfortunately I'm unaware of a way to reference code constants in the mechanim parameters list :) – Lunin Mar 22 at 2:51
• You can't. To correct what I said earlier, you change in 2 places (mechanim + constant list), not 1 place. – gjh33 2 days ago