[Solved] No boings on collision? >> needed to attach sound to Brick script in inspector

putting the audio.play(); in start worked find but its not working when I place it after void OnCollisionEnter2d (Collision2d collision)
so I must conclude I did something to prevent it detecting the collision?? My code looks pretty much like the example

any ideas??

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

private Paddle paddle;
private Vector3 paddleToBallVector;
private bool hasStarted= false;// has the game started?
private Vector2 addedForce = new Vector2 (0.5f, 0.0f); 
// how much sideways force to add to prevent stalling loop of ball with zero change in x	

void Start () {
	paddle = GameObject.FindObjectOfType<Paddle>();
	paddleToBallVector = this.transform.position - paddle.transform.position; //the offset to paddle
	print (paddleToBallVector.y);
//	audio.Play ();
}


void Update () {
	if (!hasStarted){// do while game not started
		this.transform.position = paddle.transform.position + paddleToBallVector;//locks ball relative to paddle
		if (Input.GetMouseButtonDown (0)){
			print ("left mouse button clicked so Launch Ball!");
			this.rigidbody2D.velocity = new Vector2 (2f, 10f);//shoot ball up
		//	this.rigidbody2D.AddForce(addedForce, ForceMode2D.Force);//  oops trapsball on right
			this.rigidbody2D.AddTorque(0.6f,ForceMode2D.Impulse);
			hasStarted = true;//game has started
		}
	}
	//else {//do each fram while game running
		//if(this.rigidbody2D.velocity.x==0){
		//	this.rigidbody2D.AddForce(addedForce, ForceMode2D.Force);
			// add lateral push every frame if started and x velocity ==0
			//}
		//this.rigidbody2D.velocity = this.rigidbody2D.velocity + paddle.rigidbody2D.velocity + paddle.rigidbody2D.velocity; 
		//}
}	
void OnCollisionEnter2d (Collision2D collision){
	//if (hasStarted){
		audio.Play();
	//}

}
}

There are a lot of variables here: what do you mean by “it doesn’t work”? Are there any differences to the example code here: https://github.com/CompleteUnityDeveloper/05-Block-Breaker ?

Post your full project up to github so we can download it and have a look. It’s impossible to tell just from that one class.

Cheers

thanks I’ll get to it, seem to freeze the game with the crack audio

1 Like

I you notice all the way to right in inspector looking at brick script attached to bricks there is a space for crack- you have to put the crack sound there as well as in audio source ( because we made that sound public ) and it works fine