-1

Please Guys help, I am new to Unity and programming: I have a two Bouncing Ball, tag as BouncingBall1 and BouncingBall2, I want when a bullet hit both to destroy and if the time have not exceeded displaySecond and you have destroy the balls you win, but my problem is the OnCollisionEnter is not working. The part of wining is not working the rest are, my code fragment is below.

function OnCollisionEnter(col.collision) {
    if ((displaySecond < 30) && (
            col.gameObject.tag ==
            BouncingBall1 == null &&
            col.gameObject.tag ==
            BouncingBall1 == null)) {
        print("You have won");
    }
}
Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • What do you mean it isn't working? Is the function not being called in the first place or is there an error in the code inside the function? You can add a `Debug.Log()` statement to test this. – Adam H Mar 30 '15 at 09:55
  • col.gameObject.tag == BouncingBall1 == null Uhm, no. There's something wrong here. This test has no sense. Just do col.gameObject.tag == BouncingBall1 to check if the tag is BouncingBall1. – Andrea Mar 30 '15 at 10:26
  • Sorry Guys; if i appeared dump, I was just trying to see if BouncingBall1 and BouncingBall2 have been destroys, that is why i set it to null. I have removed the null see my code below, but if I click play, immediately the console showed the Debug.log() result even if I have not destroy the Bouncing ball: please see my new code below function OnCollisionEnter(col: Collision) { if (col.gameObject.tag == "BouncingBall1" && col.gameObject.tag == "BouncingBall1") { Debug.Log("Collision"); }} – solomon ebenuwa Mar 30 '15 at 11:56
  • You observe collision. But it is not destroy function. If you want to detroy objects call [Destroy(gameObject);](http://docs.unity3d.com/ScriptReference/Object.Destroy.html) – Barış Çırıka Apr 02 '15 at 10:25
  • Thanks Baris, yes we did call Destroy(gameObject) function, but the response is that is saying its saying you have won ,when i thing is not suppose to say it. Adam H below have been very helpful and he/she is my hero – solomon ebenuwa Apr 03 '15 at 14:38

2 Answers2

0
col.gameObject.tag == BouncingBall1 == null 

I think the problem is with the way you check if an object is destroyed. This isn't the way to do it. Also, you're checking if two identical lines of code are identical. So this will always return true if the display second is lower than 30.

Try changing it to

if ( (displaySecond < 30) && 
(col.gameObject.tag == "BouncingBall1") ) { 
Debug.Log("You have won!") 
}
HoloLady
  • 1,041
  • 1
  • 12
  • 28
  • Thanks RoseHardt; when I put in your code is only the (displaySecond< 30) that is working, it showed "You have won" when displaySecond is <30 ,but the col.gameObject.tag == "BouncingBall1" will just show the "You have won " when i click play. I just wan to display You have won when displaySecond is <30 and BouncingBall1 and BouncingBall2 have been destroyed – solomon ebenuwa Mar 30 '15 at 14:25
  • You can't do that in a single if statement. How you wrote it now the bullet would have to hit both ball1 and ball2 at the same time. You could try having two booleans ball1hit and ball2hit and three if statements one checking if ball one was hit and setting the first boolean to true. And one checking if ball two was hit and setting that boolean to true. then, if boolean one and boolean two are true printing the you have won. – HoloLady Mar 31 '15 at 06:36
0

It sounds like you want keep track of whether each ball has been destroyed and display "You have won" when both have been destroyed. This code is a little crude but should achieve this:

var ball1Destroyed = false;
var ball2Destroyed = false;

...

function OnCollisionEnter(col : Collision) {
    if (col.gameObject.tag == "BouncingBall1") {
        ball1Destroyed = true;
    }
    if (col.gameObject.tag == "BouncingBall2") {
        ball2Destroyed = true;
    }
    if (displaySecond < 30 && ball1Destroyed && ball2Destroyed) {
        Debug.Log("You have won");
    }
}

This code assumes that the only collisions will be between bullets and balls (not between one ball and another), this can be done with collision layers.

Adam H
  • 1,523
  • 11
  • 21
  • Thanks Adam H, yes the collision is suppose to be between only bullet and the two balls , I have tried your code, but even when the ball is not destroyed and the displaySecond < 30 is still say "You have won" , I dont know here I have gone wrong – solomon ebenuwa Mar 30 '15 at 21:57
  • I've changed the code slightly to make the win condition more clear. I suggest you put `Debug.Log` statements inside the `if` blocks so you can see when the balls are being destroyed. Or better still, attach the debugger and put a break point inside the function so you can see when it gets hit. – Adam H Mar 31 '15 at 08:22