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

How to trigger animation on specific prefabs added to the scene

Discussion in 'Scripting' started by oracle000, Jan 17, 2021.

  1. oracle000

    oracle000

    Joined:
    May 11, 2019
    Posts:
    2
    Hi,

    (structure)
    I have a cherry prefab with a cherry script inside and I'm dragging it to put in the scene
    I have a PlayerController in Player Object that will collect the cherry object in the scene

    (scenario)
    if the player collides with the cherry object I'm calling the _player.IsItemPickUp() method in the PlayerController and since the cherry is observing the _player.IsItemPickUp, it processes the PlayerPickupCollectibles() method. My problem is the animation happens in all cherry prefabs added to the scene and not on the specific cherry prefabs that the player collides.

    I am using an observer pattern and as much as possible I don't want my class to be dependent on other classes that's why I want to implement the cherry animation in cherry class.


    Code (CSharp):
    1. public class Cherry : MonoBehaviour
    2. {
    3.     private Animator _anim;
    4.     [SerializeField] SubPlayer _player;
    5.  
    6.     void Awake()
    7.     {
    8.         _anim = GetComponent<Animator>();
    9.         StartObserving();
    10.     }
    11.  
    12.     void StartObserving()
    13.     {
    14.         _player.ItemPickUp += PlayerPickupCollectibles;
    15.     }
    16.  
    17.     void StopObserving()
    18.     {
    19.         _player.ItemPickUp -= PlayerPickupCollectibles;
    20.     }
    21.  
    22.  
    23.     void PlayerPickupCollectibles()
    24.     {
    25.         _anim.SetTrigger("CHERRY_PICKUP");
    26.     }
    27. }

    Player Controller code Snippets

    Code (CSharp):
    1. private void DetectCollider(GameObject collider)
    2.     {
    3.         if (collider.gameObject.CompareTag("Collectable"))
    4.         {
    5.             _player.IsItemPickUp();
    6.         }
    7.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    I would turn it around and put a script on the cherry that you can call to say "You got collected."

    To generalize it further, you can use interfaces and implement something like an
    ICollectable


    Code (csharp):
    1. public interface ICollectable
    2. {
    3.   void YouWereCollected();
    4. }
    Any collectable could implement that, and the player would see what they hit and ask if it has an ICollectable. You can pass interfaces to GetComponent<>() to grab MonoBehaviors that implement that interface, then call it if it exists.

    There's some cool Unity interface tutorials out on Youtube actually.
     
    oracle000 likes this.
  3. oracle000

    oracle000

    Joined:
    May 11, 2019
    Posts:
    2
    Thank you so much, your suggestion works :)
     
    Kurt-Dekker likes this.