Search Unity

Cannot set animator parameter on object right after its instantiation.

Discussion in 'Animation' started by Turnspender, Jun 2, 2015.

  1. Turnspender

    Turnspender

    Joined:
    Jun 27, 2013
    Posts:
    16
    I have a 'Door' prefab to be instantiated, and its animator has bool Open parameter. When it's false, the door is closed (default) and when it's true, door opens. I wanted some of my doors to be open right after instantiated.

    However, when I change its parameter right after its instantiation, Animator somehow switch its value back to the default and refuses to animate otherwise. Thus I cannot instantiate a door that's already open. But door works fine afterwards, such as response to the player interacting it.

    Here's simplified version of what I'm doing...

    obj = (GameObject)GameObject.Instantiate(doorPrefab, position, rotation);
    Door d = obj.GetComponent<Door>();
    d.animator = obj.GetComponent<Animator>():
    d.animator.SetBool("Open", true);

    I got true when I called GetBool right after the SetBool, but it seemed the parameter is to be switched back to false somewhere I don't know.

    I checked possibility of enabling/disabling object somewhere, but OnEnable() and OnDisable() were never called. I tried enabling object or Updating Animatior before SetBool to no avail.

    Can anyone tell me what I'm missing, please?
     
  2. Turnspender

    Turnspender

    Joined:
    Jun 27, 2013
    Posts:
    16
    I'm sorry, forget about this, it's solved. Apparently this was enabling/disabling problem. The door was disabled & enabled indirectly when its parent object did and I missed that. I somehow did something wrong with debugging to think OnEnable() and OnDisable() were never called. Ugh.