Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Artomix · Jul 04, 2014 at 02:25 PM · variableschangevaluefixmake

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!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Artomix · Jul 04, 2014 at 04:36 PM 0
Share

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!

avatar image SirCrazyNugget · Jul 05, 2014 at 12:36 AM 0
Share

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

avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Artomix · Jul 05, 2014 at 05:44 PM 0
Share

yes, if I remove static the other scripts won't be able to reach it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

(C#) Return Float Variable to previous value 1 Answer

Animation not recognizing small differences 0 Answers

Change Inspector Variables Depending On Enum 2 Answers

Value won't change/GUI won't draw 1 Answer

Editor Scripting: How To Detect a Change in Player Settings 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges