Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Scripting' started by HBK_, Dec 25, 2017.

Thread Status:
Not open for further replies.
  1. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    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...
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Are you sure you aren't getting an error in the console?
     
  3. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    Yeap! 100% sure.....been staring at him for like 2 days hoping to get some error but nothing!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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. :)
     
  5. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    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!...
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    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.
     
    Last edited: Dec 26, 2017
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can post your code , in case something is off with it, maybe someone can notice..
     
  8. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    ya sure here are the codes that i tried
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shooting : MonoBehaviour {
    6.  
    7.     public float damage = 25f;
    8.            Animator anim;
    9.  
    10.     void Start(){
    11.                 anim = GetComponent<Animator>();
    12.     }
    13.  
    14. // I simply call this Fire method from my button's OnClick() to fire and i get the debug log "Fired"
    15.     public void Fire() {
    16. // I Tried everything from seting a bool to true to changing float,etc etc
    17.                 anim.SetTrigger("Fire");
    18.         Debug.Log ("Fired!");
    19.         Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
    20.         Transform hitTransform;
    21.         Vector3   hitPoint;
    22.  
    23.         hitTransform = FindClosestHitObject(ray, out hitPoint);
    24.  
    25.         if(hitTransform != null) {
    26.  
    27.             Health h = hitTransform.GetComponent<Health>();
    28.  
    29.             while(h == null && hitTransform.parent) {
    30.                 hitTransform = hitTransform.parent;
    31.                 h = hitTransform.GetComponent<Health>();
    32.             }
    33.  
    34.  
    35.             if(h != null) {
    36.                 h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.All, damage);
    37.  
    38.             }
    39.         }
    40.  
    41.     }
    42.  
    43.     Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) {
    44.  
    45.         RaycastHit[] hits = Physics.RaycastAll(ray);
    46.  
    47.         Transform closestHit = null;
    48.         float distance = 0;
    49.         hitPoint = Vector3.zero;
    50.  
    51.         foreach(RaycastHit hit in hits) {
    52.             if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
    53.  
    54.  
    55.                 closestHit = hit.transform;
    56.                 distance = hit.distance;
    57.                 hitPoint = hit.point;
    58.             }
    59.         }
    60.  
    61.  
    62.  
    63.         return closestHit;
    64.  
    65.     }
    66. }
    67.  
    here's the second code
    Code (CSharp):
    1.  using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class Animtest : MonoBehaviour, IPointerClickHandler
    6. {
    7.  
    8.      public void OnPointerClick (PointerEventData eventdata)
    9.      {                GetComponent<Animator>().Play("Fire");
    10.  
    11.              
    12.          Debug.Log (gameObject + " clicked");
    13.  
    14. }
    15. }
    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
    Code (CSharp):
    1. bool shoot = false;
    2. Animator anim;
    3.  
    4. void Start(){
    5.  
    6.     anim = GetComponent<Animator>();
    7.  
    8. }
    9. void update() {
    10.  
    11.    if(shoot == false){
    12.    Debug.LogError("Its False Dude!");
    13. }
    14.  
    15.     if(shoot == true){
    16.    //Again i tried everything from setting bools to true to changing floats etc  
    17.     anim.SetTrigger("Fire");
    18.  
    19.     Debug.LogError("The is triggered to be True and the Fire trigger is triggered");
    20.    //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.
    21.    }
    22. }
    I Change The Bool to True when the fire method gets called by button.
     
    Last edited: Dec 27, 2017
  9. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    Tried that too bro!...I Inserted the code Above..take a look
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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?
     
  11. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    it'll be more like a 2 GB of Unity Package!...:) 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?
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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. :)

    package only needs:
    game object with animator
    animation
    button
    script.
    :)
     
  13. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    okay i'll try to package and share it!
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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. :)
     
  15. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,065
    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.
     
  16. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    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?
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Um...Not sure what to say about that :)
    I can't think of anything else to help you, sorry.
     
  18. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    ya i understand!...BTW Thank You So Much Bro!
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)
     
  20. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    i'm glad that anybody came to help me.That means a lot!...PEACE!
     
  21. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It would help to see the input code that works. Also, have you tried setting a bool in the animator instead? You title says you did it, but did you do it using setBool in the animator? There is very little information on setTrigger in the script index.
     
    Last edited: Dec 27, 2017
  22. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    quote from my previous scripts...it seems like a problem with my unity or something...But Thank you so much bro for helping me! PEACE
     
  23. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Well, good luck. Something is making it dependent on input. You would have to probably start over with a small project and change animation through script to see if you can do it, which I'm sure you can. Everyone would be screaming if they could only change an animation from input.
     
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This is a guess, but if it's not one of the previously mentioned issues, could it be maybe because of your setup (and networking/photon)?
    If you tried to link the button's OnClick() event to a script on a prefab, for instance.. that would not work.
     
  25. dildobishop69

    dildobishop69

    Joined:
    Feb 18, 2023
    Posts:
    1
    If the problem persists after trying everything ,deleting and recreating the animator controller from the beginning works ,which sucks.
     
  26. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,470
    I really hope they haven't been stuck for 7 years ;)

    Please don't necro old threads.
    Thread locked
     
Thread Status:
Not open for further replies.