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

Question Animations Problem

Discussion in 'Scripting' started by agkikas2000, Aug 22, 2023.

  1. agkikas2000

    agkikas2000

    Joined:
    Apr 1, 2023
    Posts:
    3
    Hello, im new to unity and C# in general, im trying to make a door system, basically what im trying to do, is for the doors to open when a player enters a trigger area.

    I have this simple script here:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AnimationTrigger : MonoBehaviour
    4. {
    5.     public AnimationClip animation1Clip;
    6.     public AnimationClip animation2Clip;
    7.     public GameObject animation1Start;
    8.     public GameObject animation2Start;
    9.  
    10.     private void OnTriggerEnter(Collider other)
    11.     {
    12.         if (other.CompareTag("Player"))
    13.         {
    14.             PlayAnimations();
    15.         }
    16.     }
    17.  
    18.     private void PlayAnimations()
    19.     {
    20.         Animation animation1 = animation1Start.GetComponent<Animation>();
    21.         animation1.clip = animation1Clip;
    22.         animation1.Play();
    23.  
    24.         Animation animation2 = animation2Start.GetComponent<Animation>();
    25.         animation2.clip = animation2Clip;
    26.         animation2.Play();
    27.     }
    28. }
    29.  

    I know this is NOT the best way to handle animations in unity, but im super new, and im trying to do something simple and honesty i dont know how to work well with animations yets.

    So i have the script assigned to the trigger area, with the animations and the game objects assigned on the script, and i have an Animation component in both game objects (Basically a sliding glass door that has 2 parts)

    The problem im facing is when ever the player walks in the trigger area this pops up in the unity console:

    Default clip could not be found in attached animations list.
    UnityEngine.Animation:play ()
    AnimationTrigger:playAnimations () (at Assets/Scripts/AnimationTrigger.cs:26)
    AnimationTrigger:OnTriggerEnter (UnityEngine.Collider) (at Assets/Scripts/AnimationTrigger.cs:14)

    Thank you so much for your help in advance!!
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Kurt-Dekker and enesbagci2332 like this.
  3. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    One of the problems with your code is your animation1 and animation2 are using the same component.

    Try

    Animation animation1 = animation1Start.GetComponents<Animation>()[0];

    Animation animation1 = animation1Start.GetComponents<Animation>()[1];

    Also try using Unity's animation system, with adding bools to your code. After you do these update us with your code!

    Edit: Check the url @wideeyenow_unity has sent!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  5. agkikas2000

    agkikas2000

    Joined:
    Apr 1, 2023
    Posts:
    3

    Hey, so i did end up making it work using this script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AnimationTrigger : MonoBehaviour
    4. {
    5.     public AnimationClip animation1Clip;
    6.     public AnimationClip animation2Clip;
    7.     public GameObject animation1Start;
    8.     public GameObject animation2Start;
    9.  
    10.     private void OnTriggerEnter(Collider other)
    11.     {
    12.         if (other.CompareTag("Player"))
    13.         {
    14.             PlayAnimations();
    15.         }
    16.     }
    17.  
    18.     private void PlayAnimations()
    19.     {
    20.         Animation animation1 = animation1Start.GetComponent<Animation>();
    21.         Animation animation2 = animation2Start.GetComponent<Animation>();
    22.  
    23.         if (animation1 != null && animation2 != null)
    24.         {
    25.             animation1.clip = animation1Clip;
    26.             animation1.Play();
    27.  
    28.             animation2.clip = animation2Clip;
    29.             animation2.Play();
    30.         }
    31.     }
    32. }
    After asigning animations again and remaking them, the doors now open and close correcty. So for now im gonna take the win. Later on though i might rework the whole thing using the link @wideeyenow_unity send.

    Thank you so much for your help!!!!!
     
  6. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    Good job dude! Don't forget to change title's prefix to resolved! Have a great day!
     
    agkikas2000 likes this.