Not Able To change bool,float,trigger or anything in animator using UI Button!.

Hi, I’m much of a noob here so,i have animations set in my animator and animator component attached to the Player and conditions set in animator…my problem is i wanna trigger,set bools etc…make transition from idel animation to fire using the button…I tried everything from using the event system to play a string to scripting and nothing worked…but when i put the anim.SetTrigger(“Fire”); it works when i assign it to keyboard input it again works but when i put the same code to my Fire Script which gets executed when i press the button…the script gets called and the anim.SetTrigger(“Fire”) gets called in debug but nothing happens it doesn’t trigger the Fire trigger which will make the transition…i even tried to make bools inside the script so when the fire thing gets called set the bool to true which will trigger the anim.SetTrigger(“Fire”); in update but nothing happened at all.

i also tried putting animator component in UI Button’s OnClick() and made scripts with public so that they can be called by OnClick() but nothing happened again!..

Any Help Will Be Appreciated…

Are you sure you aren’t getting an error in the console?

Yeap! 100% sure…been staring at him for like 2 days hoping to get some error but nothing!

Having a public method that is called from the Button’s OnClick event should work, no problem.
Maybe post your code in case something is causing an issue. :slight_smile:

ya i already did that…Making the method public so it can be called by the UI Button’s OnClick() and getting the Animator component at the very start of script,but as i said no luck!..

You can open the animator window before play, and if you highlight the object it’s connected to, it may give you more information. If the method is being called, you have to find out why the animator controller is ignoring it, or not getting it. Other than that, I have no idea. Basically, it should work. A method is a method and Unity doesn’t care if a button activated it or a input activated it. It might have something to do with the name fire. I think that gets used by input, so you may be using it in a way that isn’t giving the results you think it is. Anyway, there is something different about your use of input and your use of the button that is causing a logical difference in results.

You can post your code , in case something is off with it, maybe someone can notice…

ya sure here are the codes that i tried

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting : MonoBehaviour {

    public float damage = 25f;
           Animator anim;

    void Start(){
                anim = GetComponent<Animator>();
    }

// I simply call this Fire method from my button's OnClick() to fire and i get the debug log "Fired"
    public void Fire() {
// I Tried everything from seting a bool to true to changing float,etc etc
                anim.SetTrigger("Fire");
        Debug.Log ("Fired!");
        Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
        Transform hitTransform;
        Vector3   hitPoint;

        hitTransform = FindClosestHitObject(ray, out hitPoint);

        if(hitTransform != null) {

            Health h = hitTransform.GetComponent<Health>();

            while(h == null && hitTransform.parent) {
                hitTransform = hitTransform.parent;
                h = hitTransform.GetComponent<Health>();
            }


            if(h != null) {
                h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.All, damage);

            }
        }

    }

    Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) {

        RaycastHit[] hits = Physics.RaycastAll(ray);

        Transform closestHit = null;
        float distance = 0;
        hitPoint = Vector3.zero;

        foreach(RaycastHit hit in hits) {
            if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {


                closestHit = hit.transform;
                distance = hit.distance;
                hitPoint = hit.point;
            }
        }



        return closestHit;

    }
}

here’s the second code

 using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
public class Animtest : MonoBehaviour, IPointerClickHandler
{

     public void OnPointerClick (PointerEventData eventdata)
     {                GetComponent<Animator>().Play("Fire");
  
             
         Debug.Log (gameObject + " clicked");

}
}

i get the debug logged from this script too.

The names of animations,triggers are all correct i triple checked them but when i insert the “If input get keydown bla bla” and when i press it the animation works smoothly but when i put that anim.SetTrigger(“Fire”); in Fire method and call the fire method from button’s OnClick() it doesn’t works.

I Also tried adding bools Here’s the code for that

bool shoot = false;
Animator anim;

void Start(){

    anim = GetComponent<Animator>();

}
void update() {
 
   if(shoot == false){
   Debug.LogError("Its False Dude!");
}

    if(shoot == true){
   //Again i tried everything from setting bools to true to changing floats etc  
    anim.SetTrigger("Fire");
 
    Debug.LogError("The is triggered to be True and the Fire trigger is triggered");
   //first i try if it sets the trigger to true and it doesn't if it ever did then i'll make bool "Shoot" false at the end.
   }
}

I Change The Bool to True when the fire method gets called by button.

Tried that too bro!..I Inserted the code Above…take a look

Not sure what to say. If you say the animation is working under other circumstances, I’m at a loss.
Can you post a simple repro unity package in the thread to look at?

it’ll be more like a 2 GB of Unity Package!..:slight_smile: when i add the input.getkeydown something and add anim.settrigger it works.It just don’t works with ui button…any idea what should i do?

Obviously by simple I mean just the necessary code to reproduce your error. That’s not 2GB.
Beyond that responses already here in the thread, I cannot think of anything, which is why I suggested/asked about a small package. Perhaps there is something overlooked, that hasn’t been thought of, that would come to mind seeing the setup. :slight_smile:

package only needs:
game object with animator
animation
button
script.
:slight_smile:

okay i’ll try to package and share it!

Cool… If you can reproduce it in the package, I’ll try to have a look at it.
If you cannot reproduce it in the package, then I suspect the cause may lie elsewhere in your project. :slight_smile:

It should work as described. A couple of quick things to double check for sanity (they sound simple, but are easy to miss):

  1. make sure your animator is enabled. When you edit your timeline, the component is disabled. If you edit and click play sometimes it won’t re-enable. It’s super easy to overlook.

  2. triple check the the trigger name, make sure it actually a trigger and not a bool and spelled exactly the same way. “Fire” not “fire” and that there aren’t any spaces in the name (leading or trailing).

again, super simple, but easy to miss things. Also you might post a screenshot of your animator window.

Checked All These and they are absolutely right.When i change the input from touch to button,i mean when i add Input.getbutton etc etc…and add the anim.set…etc to it,it works but after putting anim.set… to fire method it don’t.I don’t know is there anything wrong with my unity or project. methos5k bro my unity crashes when i try to make the package of those certain things,I’m using Unity 5.6.4f1 on Laptop which has Intel I5 Processor,8GB Ram and almost 40GB+ Free In C Drive…I Guess its my unity thats not working?

Um…Not sure what to say about that :slight_smile:
I can’t think of anything else to help you, sorry.

ya i understand!..BTW Thank You So Much Bro!

No problem. Sorry you couldn’t get it working, yet. Must be something odd, or overlooked b/c it should work as-is.
Take care, enjoy the rest of your game making :slight_smile:

i’m glad that anybody came to help me.That means a lot!..PEACE!