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. Dismiss Notice

Help Changing Object Animation

Discussion in 'Scripting' started by ccretrberk, Feb 11, 2018.

  1. ccretrberk

    ccretrberk

    Joined:
    Feb 8, 2018
    Posts:
    7
    Hello, i have a animated object which name is "Dancing" and i have 6 different animation.

    i want to do change animations with UI Buttons.

    what is the wrong with this code?

    using UnityEngine;
    using System.Collections;

    public class ExampleClass : MonoBehaviour
    {
    public GameObject Dancing;
    void Example()
    {
    GetComponent<Animation>().Play();
    }
    }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is this script on that game object?
    if not, you must use GetComponent on the 'Dancing' game object.
    Code (csharp):
    1. Dancing.GetComponent<Animation>().Play();
    However, if you have 6 animations, you may need/want to supply the name of the one you want to play.

    https://docs.unity3d.com/ScriptReference/Animation.Play.html
     
    ccretrberk likes this.
  3. ccretrberk

    ccretrberk

    Joined:
    Feb 8, 2018
    Posts:
    7
    still cant reach the object(Dancing)'s animation

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Animation_Change : MonoBehaviour {
    public GameObject Dancing;

    public void ChangetoAnimation(string animationToChangeTo)
    {
    Dancing.GetComponent<Animation>().Play();

    Application.LoadLevel(animationToChangeTo);


    }
    }
     
  4. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Do you have an animator component attachex to the gameobject with the animation in it?
     
    ccretrberk likes this.
  5. ccretrberk

    ccretrberk

    Joined:
    Feb 8, 2018
    Posts:
    7
    i dont think so, how can i
     
  6. ccretrberk

    ccretrberk

    Joined:
    Feb 8, 2018
    Posts:
    7
    ok thank you for helps.. i found how attached and i used

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Animation_Change : MonoBehaviour {
    public GameObject Dancing;

    public void ChangetoAnimation(string animationToChangeTo)
    {
    Dancing.GetComponent<Animation>().Play(animationToChangeTo);

    it worked.. yeppaaaaa