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

Unity 5 Animation not working

Discussion in 'Scripting' started by lupo, May 11, 2015.

  1. lupo

    lupo

    Joined:
    Apr 4, 2009
    Posts:
    43
    Hi All,

    I'm using the new GetComponent.<Animation>().Play() technique to no avail. Here's the script (js):

    Code (JavaScript):
    1. private var currentDoor : GameObject;
    2. function OnTriggerEnter(col : Collider) {
    3.    if(col.gameObject.tag == "Player"){
    4.       Debug.Log("Player entered trigger!");  
    5.       currentDoor = (GameObject.Find("HoverStation"));
    6.       currentDoor.GetComponent.<Animation>().Play("dooropen");
    7.    }
    8. }
    Things I've verified:
    • I've set the animation type to Legacy
    • My animations preview well in the Inspector
    • The animation name is correct
    • Debug.Log is letting me know that "Player entered trigger"
    • I can Destroy the "currentDoor" game object with no problem - I did this just to makie sure I was accessing it correctly, by using Destroy(currentDoor).

    So I'm at a loss. Anyone have a clue?
     
  2. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Do you get any errors ?
     
  3. lupo

    lupo

    Joined:
    Apr 4, 2009
    Posts:
    43
    Nope, no errors.
     
  4. lupo

    lupo

    Joined:
    Apr 4, 2009
    Posts:
    43
    I should mention I'm on 5.01f1
     
  5. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    I did a small test to check this out and I managed to get my test working with a setup quite similar to yours, replace the block inside the Player tag check with the code below and let me know what happens
    Code (CSharp):
    1. currentDoor = (GameObject.Find("HoverStation"));
    2. var anim : Animation;
    3. anim = currentDoor.GetComponent.<Animation>();
    4. anim.Play("dooropen");
    no guarantee this would work for you as i'm not quite sure how you have your setup
     
  6. lupo

    lupo

    Joined:
    Apr 4, 2009
    Posts:
    43
    Interesting idea, 5v, thanks for trying. No luck though.
     
  7. lupo

    lupo

    Joined:
    Apr 4, 2009
    Posts:
    43
    Ran another test, was able to get a different animation to run continually as long as it is in an Update function, or as long as I set Wrap mode to Loop, like so (but playing "dooropen" still eluding me):

    Code (Javascript):
    1. function Update () {
    2. GetComponent.<Animation>().Play();
    3. }
    Even the following script, attached directly to the HoverStation, will not animate the door. FYI the HoverStation is the parent of the door and the building mesh.

    Code (Javascript):
    1. function Update () {
    2. GetComponent.<Animation>().Play("dooropen");
    3. }
     
  8. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Oh in that case it makes sense, does the door (child of HoverStation) have a animation component added to it or is its animation handled by the HoverStation which would be so if its all one model
     
  9. lupo

    lupo

    Joined:
    Apr 4, 2009
    Posts:
    43
    Tried it again with just a door object created in 3ds Max. Of course Max and Unity don't play perfectly well in terms of Legacy animations, a game object with a move animation will for some stupid reason transport itself to 0,0,0 in your game. So you have to make another object that does not move, in my case I made a box. I then exported both as an FBX, and called using this code - attached this code to the parent object:

    Code (JavaScript):
    1. function OnTriggerEnter(col : Collider){
    2.   if(col.gameObject.tag == "Player"){
    3.        currentDoor = (GameObject.Find("door"));
    4.        currentDoor.GetComponent.<Animation>().Play("dooropen");
    5.   }
    6. }
    It works this time, which leads me to believe there is something amiss in my HoverStation setup. At least this gets me by for now.