Search Unity

Cant add animation to inspector

Discussion in 'Scripting' started by warrenbrandt, Apr 18, 2020.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    i have

    public Animation anim;

    and in my update

    i have an if (isRunning) {
    anim.Play("Run");

    im not able to drag the animator into the slot in the inspector

    and im getting an error when i click on run bool in the inspector

    "
    UnassignedReferenceException: The variable anim of LeftRightPlatformPatrol has not been assigned.
    You probably need to assign the anim variable of the LeftRightPlatformPatrol script in the inspector.
    UnityEngine.Animation.Play (System.String animation)

    but i definitely have an animator attached to the object and the name of the animation is correct, i just dont understand this.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Did you mean this?
    Code (CSharp):
    1. public Animator anim;
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    As @PraetorBlue pointed out above, there are two things and they are VERY similarly-named:

    Animator - an instance of a MecAnim core state machine object
    Animation - a single Animation clip that does something.

    Animations may be embedded into Animators, then you tell the Animator "Hey, change to walk state!" and it will do the right Animation, if set up correctly.

    It is probably worthwhile going through a MecAnim tutorial. It's super-powerful and could make life easier for you, especially once you get a lot of animations.

    Technically it IS possible to use the Legacy animation system and just use Animations directly and purely, but there is a ton more work to keep track of, IMNSHO. Unity might even stop supporting it sometime...
     
    QwertyCodingGames likes this.
  4. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    oh for goodness sake, just got intelisense sorted for my vs19...i feel like such an idiot...thanks
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Eh, don't worry about it... I still sometimes say one when I mean the other. I try to think about the Animator as an "ator" in my head, so I will tend to call it "ator" instead of Animator in conversation.

    I make up a lot of words actually, and sometimes some of them make sense. I think ator makes sense for Animator.

    Hm, then I guess you could call an Animation a "shun..."

    "Ey Warren, hook up the all the shuns to the ator, let's go fly this spaceship!"

    Precise professional technical terminology always. ;)
     
    Last edited: Apr 18, 2020
    warrenbrandt likes this.
  6. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Ha ha that actually makes a lot of sense. Hey Kurt im really battling with my NPC's, trying to get him to patrol , put his arm out when the player gets close, down again when the player moves away, detect when the player is landed, move towards the player when hes close enough, i currently have a spaghetti bowl full of bools and if statements it seems really perplexing trying to get all this working with the animations etc, whats the best way to do this?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    I feel your pain! Spaghetti is delicious but not so much in patrol AI code.

    Knowing nothing more than you say above, here are my thoughts: You gotta break the problem down.

    I see three distinct scripts with only a little bit of awareness of each other.

    First of all, Animator states for your rescuee:

    - idle
    - idle waving
    - walking

    Script 1 - patrolling the platform:

    go to waypoints (set ator walking) in some kind of order
    if no waypoints here (small platform), just idle
    maybe stop at a waypoint for a few seconds (set ator idle)
    periodically check for player landed
    if see player landed, goto player (disable Script 2 below)
    if player lifts off, return to walking, enable Script 2 below

    Script 2 - beckon the player

    periodically check distance to player for waving (ignore player landed status, just distance)
    if less than this distance, set ator to idle waving
    This script gets disabled by Script 1 above when the player lands

    Script 3 - board the ship

    IF:
    - the rescuee ever gets within a small range of the ship
    - the player ship IS landed
    THEN:
    - turn off above scripts and tween into the ship
    It's easiest if this boarding cannot be interrupted / reversed: make the tween quick and even if the player lifts off, the tween succeeds from original point at start of this to arrival in ship
    Alternately you can temporarily turn off player controls while rescuee is boarding
    When the rescuee boards, destroy him and update your onboard count.

    Final thoughts:

    I'd put all three scripts on the rescuee and let them find each other, or else drag references into each other. Here is what they need to tell each other:

    script 1 disables script 2 when going to ship
    script 2 enables script 2 when ship out of reach
    script 3 disables both script 1 and 2 when activated by close proximity

    FINAL FINAL thoughts:

    in the Update() of each of these I would have two sections, probably two separate functions:

    Decide();
    Enact();

    Decide would set local boolean states on and off

    Enact would just do the moving or set the ator bools, etc.

    The really cool part about that last thing is you can implement a timer that only runs Decide once every random amount of time, like 0.5 to 2.0 seconds randomly.

    That way if there are five rescuees waiting, they don't all instantaneously lockstep start moving; it makes them seem like real agents.

    I WANNA PLAY YOUR GAME! GO WARREN!

    Also, I have this setup (pretty much) in my Impulse One game. I'll make a video here and show you, or you can grab it off the appstore or google play... just search Kurt Dekker Impulse One.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Here is pretty much what I described above:



    I didn't implement this QUITE as cleanly as what I wrote in the above post, but if I did it today I would separate it into those three scripts.

    Here is another similar view that shows off the seasonal Christmas theming:

     
    Last edited: Apr 18, 2020
  9. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Hey buddy I cant thank you enough, you been really helpful!!! and could i play any of your games?
     
    Kurt-Dekker likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
  11. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Thats very interesting, i was wondering about loading and unloading particular scripts and wasnt sure if things were done that way...hey looking at your videos i am jealous!!!

    you should really setup a youtube channel, though im sure you prob dont have time. thanks again mr Dekker!
     
    Kurt-Dekker likes this.
  12. HSxxx1

    HSxxx1

    Joined:
    Aug 30, 2021
    Posts:
    1
    Code (CSharp):
    1. Public Animation anim;
    But when i am trying to drag animation in the slot , it is not happennig and no other animation is showing the slot.
    Can someone please help me ??
     
  13. Marvin4213

    Marvin4213

    Joined:
    Nov 1, 2021
    Posts:
    1
    Hi, had the same problem, try using a
    Code (CSharp):
    1. public Motion anim;
    .
     
    Last edited: Feb 8, 2023