Question by
watercolorheartdev · Sep 27, 2017 at 02:57 AM ·
audiosoundaudioplay
How do I have one object play another object's sound? (C#)
This is the code for my brick in a Breakout clone and the sound doesn't play:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Redblock : MonoBehaviour {
public GameController gameController;
public float targetTime = 5.0f;
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "ball" && col.gameObject != null)
{
gameController.Score += 15;
AudioSource bup = GetComponent<AudioSource>();
bup.Play(); // plays sound when collided.
}
gameObject.SetActive(false);
Destroy(gameObject, 3f);
}
}
So I want to attach the code to the ball instead. How do I go about this?
Comment
Your answer