- Home /
variables reset value
Hi all! I am making a old school pong game, and I am quite stuck now. i have made that when player01 scores 10 points, a new scene will show up, saying that this team has won. But if you return to the Main Menu by clicking a button, and play the game again, the score won't reset. Can someone please help me out? Here are the scripts I use:
//for the score
#pragma strict
static var playerScore01 : int = 0;
static var playerScore02 : int = 0;
var theSkin : GUISkin;
var theBall : Transform;
function Start () {
theBall = GameObject.FindGameObjectWithTag ("Ball").transform;
}
static function Score (wallName : String) {
if (wallName == "rightWall")
{
playerScore01 += 1;
}
else {
playerScore02 += 1;
}
if (playerScore01 == 10) {
Application.LoadLevel("OrangeWins");
Debug.Log("OrangeWins");
}
if (playerScore02 == 10) {
Application.LoadLevel("OrangeWins");
Debug.Log("PurpleWins");
}
if (Application.LoadLevel == true) {
playerScore01 = 0;
playerScore02 = 0;
}
Debug.Log("Player Score 1 is" + playerScore01);
Debug.Log("Player Score 2 is" + playerScore02);
}
function OnGUI () {
GUI.skin = theSkin;
GUI.Label ( new Rect (Screen.width/2-150-18, 20, 100, 100), "" + playerScore01);
GUI.Label ( new Rect (Screen.width/2+150-18, 20, 100, 100), "" + playerScore02);
// if (GUI.Button ( new Rect (Screen.width/2-121/2, 35, 121, 53), "RESET")) {
// playerScore01 = 0;
// playerScore02 = 0;
// theBall.gameObject.SendMessage ("ResetBall");
// }
}
//for the camera
#pragma strict
var mainCam : Camera;
var topWall : BoxCollider2D;
var bottomWall : BoxCollider2D;
var leftWall : BoxCollider2D;
var rightWall : BoxCollider2D;
var Player01 : Transform;
var Player02 : Transform;
function Start () {
//move each wallto its edge location:
topWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f ,0f)).x,1f);
topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 (0f, Screen.height, 0f)).y + 0.5f);
bottomWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f ,0f)).x,1f);
bottomWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y -0.5f);
leftWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
leftWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
rightWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
rightWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
Player01.position.x = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)).x;
Player02.position.x = mainCam.ScreenToWorldPoint (new Vector3 (Screen.width -75f, 0f, 0f)).x;
}
//for the ball #pragma strict
var ballSpeed : float = 100;
function Start () {
yield WaitForSeconds (2);
GoBall();
}
function Update () {
var xVel : float = rigidbody2D.velocity.x;
if (xVel < 18 && xVel > -18 && xVel !=0) {
if (xVel > 0) {
rigidbody2D.velocity.x = 20;
}
else {
rigidbody2D.velocity.x = -20;
}
Debug.Log ("Velocity Before " + xVel);
Debug.Log ("Velocity After " + rigidbody2D.velocity.x);
}
}
function OnCollisionEnter2D (colInfo : Collision2D) {
if (colInfo.collider.tag == "Player") {
rigidbody2D.velocity.y = rigidbody2D.velocity.y/2 + colInfo.collider.rigidbody2D.velocity.y/3;
audio.pitch = Random.Range(0.8f, 1.2f);
audio.Play();
}
}
function ResetBall () {
rigidbody2D.velocity.y = 0;
rigidbody2D.velocity.x = 0;
transform.position.x = 0;
transform.position.y = 0;
yield WaitForSeconds (1);
GoBall();
}
function GoBall () {
var randomNumber = Random.Range(0, 2);
if (randomNumber <=0.5) {
rigidbody2D.AddForce (new Vector2 (ballSpeed, 10));
}
else {
rigidbody2D.AddForce (new Vector2 (-ballSpeed, -10));
}
}
//for the Back buttons in the menu.
var levelToLoad : String;
var soundhover : AudioClip;
var beep : AudioClip;
var QuitButton : boolean = false;
function OnMouseEnter(){
audio.PlayOneShot(soundhover);
}
function OnMouseUp(){
audio.PlayOneShot(beep);
yield new WaitForSeconds(0.35);
if(QuitButton){
Application.Quit();
}
else{
Application.loadLevel("pong");
}
}
@script RequireComponent(AudioSource)
//Application.loadlevel("LevelToLoad"); **DOES NOT WORK**
Thanks for the Help!!
Answer by SirCrazyNugget · Jul 04, 2014 at 02:33 PM
Just place playerScore01 = 0 and playerScore02 = 0 on your score file in both positions 22 and 26 and scrap the if(Application.LoadLevel) on line 31 to 34
thanks for the quick reply, but if I change the playerScore01=0; and the playerScore02=0; on line 22 and 26 the code of the ten points won't work anymore. What do you exactly mean with on your score file in both positions 22 and 26...? thanks!
if you change your Score function to be
static function Score (wallName : String){
if(wallName == "rightWall"){
playerScore01 += 1;
}else{
playerScore02 += 1;
}
if(playerScore01 == 10){
EndLevel (1);
}
if(playerScore02 == 10){
EndLevel (2);
}
}
static function EndLevel(winningPlayer : int){
Debug.Log("Winner:" + winningPlayer);
Debug.Log("Final Score was Player 1:" + playerScore01 + ", Player 2:" + playerScore02);
playerScore01 = 0;
playerScore02 = 0;
Debug.Log("Player Score 1 is" + playerScore01);
Debug.Log("Player Score 2 is" + playerScore02);
if(winningPlayer == 1){
Application.LevelLoad("OrangeWins");
}else{
Application.LevelLoad("PurpleWins");
}
}
The scores will then reset when EndLevel is called which also passes the winning player, (hopefully). $$anonymous$$ay also contain a typo or two
Answer by flaviusxvii · Jul 04, 2014 at 05:44 PM
static var playerScore01 : int = 0;
static var playerScore02 : int = 0;
Do you know what static means?
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics
yes, if I remove static the other scripts won't be able to reach it.