Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Play animations manually from c#

Discussion in 'Animation' started by EternalMe, Feb 27, 2020.

  1. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    I have set an animation component, and now I want to control those anims manually with c#



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class kaka : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.  
    9.     private Animation anim;
    10.  
    11.     void Start()
    12.     {
    13.         anim = gameObject.GetComponent<Animation>();
    14.         anim.Play("idle");
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        
    21.     }
    22. }
    By the i get: "The animation state idle could not be played because it couldn't be found!". Do I have to add Animation component for every Clip, to get access to them?

    I got more success when creating Animator component, and playing from there. But I don't see Blend by weight https://docs.unity3d.com/ScriptReference/Animation.Blend.html function in that class, that I need :(
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,554
    Did you actually add the idle animation to the "Animations" array in the Inspector?
     
    AcidArrow likes this.
  3. ThatCodingKid

    ThatCodingKid

    Joined:
    Aug 7, 2019
    Posts:
    1
    You misspelled Idle as idle.