Search Unity

CoinCollider won't tell Coin to animate itself

Discussion in 'Scripting' started by Nicoparadise, Feb 19, 2018.

  1. Nicoparadise

    Nicoparadise

    Joined:
    May 12, 2016
    Posts:
    7
    Here's my problem: I want to make CoinCollider, the parent gameobject which has the collision cube, to animate the child gameobject, but if i Instance the child gameobject, I can't clone it and if i pick up other gameobjects it will animate only the first gameobject to appear. If i don't do this process, the animator will animate the box collider and thus, screwing the collision detection.

    This is my code:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Coin : MonoBehaviour
    7. {
    8.     private Animator anim;
    9.  
    10.     private void Start()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.        
    14.     }
    15.  
    16.     public void DetectCollide()
    17.     {
    18.         anim.SetTrigger("isCollected");
    19.     }
    20.  
    21.          
    22. }
    23.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CoinCollider : MonoBehaviour {
    6.     public AudioClip Collected;
    7.     public Coin coin;
    8.     private void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.tag == "Player")
    11.         {
    12.             AudioSource.PlayClipAtPoint(Collected, transform.position);
    13.             ScoreManager.Instance.GetCoin();
    14.             Destroy(this.gameObject, 1f);
    15.         }
    16.     }
    17. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I don't really understand your explanation (about not being able to clone). But if you have a parent with CoinCollider and a child with Coin on it, you can use GetComponentInChildren<Coin>() to get the coin instance from the child and then call the DetectCollide on it.

    You don't need to setup a public reference for it. You could still do that, but it's just not necessary.

    If I've got your layout confused, please explain how the coin object is setup in your scene.
     
    Nicoparadise likes this.
  3. Nicoparadise

    Nicoparadise

    Joined:
    May 12, 2016
    Posts:
    7
    So, i've got a CoinInstantiator gameobject, which gets a CoinCollider to spawn on the scene, the CoinCollider has all the logic to increment the score, and a box collider. And then, the Coin itself, which doesn't have a collider, and only gets called to animate itself. I still have to write the logic to get the coininstantiator removed. Anyway, thanks for your help, it worked!.
     
  4. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Ok I got what you where trying to say, your Coin has an animation that must be played on a child so it works properly, but the colision is on the Coin itself.

    If thats the case then you need to put the script on the coin and instead of using GetComponent<Animation>, make the field [SerializeField] and assign it on the editor.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Coin : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private Animator anim;
    8.  
    9.     public void DetectCollide()
    10.     {
    11.         if(anim != null) // just in case you forgot to assign it
    12.             anim.SetTrigger("isCollected");
    13.         else
    14.             Debug.LogError("Forgot to assign the animator");
    15.     }
    16.        
    17. }