Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

start an animation without state name or trigger

Discussion in 'Scripting' started by mahdiii, Aug 14, 2017.

  1. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    Hi all
    I would like to start the first animation connected to start state without name or trigger parameter.
    I have to define the same trigger parameter for all animations to start from start state?
    thx in advance
     
    Last edited: Jun 13, 2022
  2. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    I dont get your point!!

    it works already like you want, the default state will be triggered and played automatically
     
  3. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    yes but I dont want only one time! I have several states, so it transits to other states and when I activate the gameobject, it is not started from the first state
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Wow I still don't get it.
    So you have a set of states, IE: FirstState, SecondState, ThirdState.
    you have an gameObject that plays an animation FirstState, Then you active the gameObject. but wait if it wasn't active how did it play the FirstState? But if you did activate the gameObject it's not playing the FirstState. So what is it animation is it playing? no animation? the SecondState or ThirdState?
     
  5. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    wow wow wowwwww!!!
    yes I have several animations I said before!!!
    only one time (for the first time when the game object is activated) it is started from the first animation(s1)
    start-> s1->s2->...
    but for the second,third etc time when my gameobject is activated, it is in the last animation
    I can use animator.play
    but I don't want to use state name , I want when the gameobject is activated, my animation is started from the first state without any conditions (like name,etc)
    my question is so clear!!!
     
    tryano1 likes this.
  6. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    you can have only one state that is connect to start node. I want to play this animation when I want without statename or trigger activation
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    it sounds like you need to look into OnEnable()
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html

    it's kind of like Start() except it runs when the gameObject is activated

    as far as the playing the first animation every time.
    You may have to provide some of your code to get a better answer on this. I don't know how you are currently switching between the animation.
    Do you have the list of animation is some kind of Array or List<>.
     
  8. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    my problem is not OnEnable or Start!!!
    "you can have only one state that is connect to start node. I want to play this animation when I want without statename or trigger activation"
     
  9. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    from what you said you cant do that!!

    you can play animation, by activating the state, and there is 2 ways to do that calling Play("statename") or by Transition (trigger,bool, or any other condition)

    if you can give more details about what you are trying to acheive, probably we could help!!!
     
  10. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    So let me try again to understand.
    you have a gameObject, it's currently active = false.
    then if activate = true, for the first time, play animation s1, then activate = false
    then if activate = true, for the second time, play animation s2, then activate = false
    then if activate = true, for the third time, play animation s3, then activate = false
    then if activate = true, for the forth time, play animation s4, then activate = false

    is this what you're after?
     
  11. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    yes but I always want to play the first state s1 when I activate the game object.
    startnode->s1->s2->s3
    I said I can write animator.play("s1_state",0,0);
    but if I have 1000 animators with different first state name(s1_state for animator1,a1_state for animator2,b1_state for animator3) I need to define a string variable (state name) and change it for every animator!
    but unity should have a simple function that can execute from the first without anything
     
  12. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    can you tell me how you are determining witch animation is the first animation? if you have 1000 animation and each gameOject plays a different first animation. you have to specify that same where. you can create a public animation variable to set the first animation. you could create a relational dictionary to link statename with first animation

    good luck with this. I'm all out of ideas and questions. you're not giving enough info to help.
     
  13. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Ok, I dont really know how is your statemachine!!

    but in any case, what if you create a transition from anystate to your first state, which can be activated always with same parameter name, so in that case for all 1000 animator you call settrigger(sameparamName);

    and be sure that you reset all parameters you may have in the animator!!
     
  14. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    855
    yes I thought it has a simple function to reset!
    Thanks. I create a trigger parameter for all animators (same name)