want the both balls to pass y axis and then trigger game over

I’m trying to make an ping pong style game so in level 2 i have added 2 balls and i want to trigger game over only when both the balls pass beyond y axis 3.5 ::"

i’m using javascript

this is the script i’m using for the balls

“function Update ()
{
if(transform.position.y < -3.5)
{
mainGameScript.GameOver();
}
}”

and the main game script

"

unction GameOver()
{
if(score > PlayerPrefs.GetInt(“High Score” ))
{
PlayerPrefs.SetInt(“High Score” , score);
}

 Application.LoadLevel("Game_Over");

}
"

this is the whole script for the ball

"var mainGameScript : MainGame;
var particles_splash : GameObject;

function Awake()
{
rigidbody.AddForce(4, 4, 0, ForceMode.Impulse);
InvokeRepeating(“IncreaseBallVelocity”, 2, 2);

}


function Update ()

{
if(transform.position.y < -3.5)
{
mainGameScript.GameOver();
}
}

function IncreaseBallVelocity ()
{
if(rigidbody.velocity.x < 14 && rigidbody.velocity.y < 14 && rigidbody.velocity.x > -14 && rigidbody.velocity.y > -14);

{
rigidbody.velocity *= 1.05;
Debug.Log("velocity: " + rigidbody.velocity);

}
}

function OnCollisionEnter(collision : Collision)
{
Instantiate(particles_splash , transform.position , transform.rotation);
audio.Play();
}"

and this is the whole script for maingame

"
var score3DText : TextMesh;
private var score : int = 0;

function Awake ()
{
InvokeRepeating(“UpdateScore” , 0.05, 0.05);
}

function UpdateScore()
{
score += 1;
score3DText.text = "Score : " + score.ToString();
}

//this is called from BonuAreaDetectionBox.js inside OnTriggerEnter()
function AddScoreForBonusArea ()
{
score += 50;
}

function GameOver()
{
if(score > PlayerPrefs.GetInt(“High Score” ))
{
PlayerPrefs.SetInt(“High Score” , score);
}

 Application.LoadLevel("Game_Over");

}"

I don't see anything to answer... You should revise your question, it's hard to understand and possibly incomplete.

1 Answer

1

function Update () { if(transform.position.y < -3.5) { mainGameScript.GameOver(); } }

This means GameOver will be called whenever one of the two balls is out of the game, so you need to add a check for the other ball in there. You could give all balls a tag “Ball” and then do a check by using GameObject.findGameObjectsByTag(“Ball”) and then checking for each ball if it is still in the game area in a loop.

I could give you some code, but only in C#, dunno if that would help you.

thanks for d response Taschenschieber. i'm a beginner & this worked fine for me so i had another Q. it's that can i access a java script from a C# script if yes plz give me some code to modify this thank u

thanks Taschenschieber. this solved it completely