1
\$\begingroup\$

I would like to be able to run the corresponding animation (ending with "Get" in the title) for whatever item GameObject I plug into this treasurebox.

I made 2 prefabs for "item-get" animations today. Both GameObjects consist of 3 sprites: The image representing the item, and 2 lightray images that I enable/disable in alternation to get a halo effect (see animation below).

enter image description here

You can see the 2 prefabs and their corresponding animation components in the Project panel:

enter image description here

These next two images show how similar the animations are--they even both require two of the same sprites, flareA and flareB.

enter image description here enter image description here

Here is the code I am currently using to set the state of the ItemBox GameObject and run the animations. You can see that while the itemGet prefab is public (that is, I can plug in whatever gameobject I want in the editor), line 27 explicitly calls for the animation belonging only to the turd item.

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

public class ItemBox : MonoBehaviour
{

    bool disabled = false;
    private Animator anim;
    public GameObject itemGet = null;

    void Start()
    {
        anim = GetComponent<Animator>();        
        if (!disabled)
        {
            anim.Play("ItemBoxIdleState");
        }
    }

    void OnTriggerEnter (Collider other)
    {
        if (!disabled && other.gameObject.tag == "Player")
        {
            anim.Play("ItemBoxGetItemState");
            GameObject itemClone = Instantiate(itemGet, transform.position + new Vector3(0, 0.375f, 0), Camera.main.transform.rotation);
            itemClone.GetComponent<Animator>().Play("turdGet");
            Destroy(itemClone, 1.6f);
            disabled = true;
        }
    }
}

How can I change line 27 (the line before 'Destroy') so that it runs the appropriate "*Get" animation for whatever itemGet object I plug into the inspector? I think I am already close. I intend to make more items (not too many for now) and the animation I want to play will always be structured like "Get".

UPDATE 07/29/19: I tried making a public AnimationClip object in the ItemBox script and putting that name in quotes for line 27 and thw script still works, but now I get an index error saying the animation state was not found. I don't think I can plug the name of the public variable into the .Play("") quotes and make it work; I think it only worked because there is only one animation for that item anyway. I ended up removing this new variable and just keeping the .Play("") part as empty quotes and it still works. I might decide to add more animations to these item objects later so I still need to know how to specifically play the animation that ends in "Get".

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.