Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 alexandereturns · Jul 23, 2017 at 01:05 PM · countercounting

Script stops counting after 1 count.


Hello. It has been 4-5 days since i started practicing Unity and c#. I am pretty ameteur. I have an issue that i could not figure out. I am trying to code that simple 2D ping-pong like game. Every time player misses the ball i instantiate the ball and count the score. But even the ball is instantiated everytime , game stops increasing the scores after 1 time.

 `
 using System.Collections;
 
 using System.Collections.Generic;
 
 using UnityEngine;
 
 using UnityEngine.UI;
 
 
 
 public class ballmove : MonoBehaviour {
 
     private Rigidbody2D rb;
 
     public float speed;
 
     public GameObject top;
 
     private Vector2 direct;
 
     public int p2;
 
     public int p1;
 
     public Text  scoretext;
 
     public Text  scoretext2;
 
 
 
     void Start () {
 
 
             rb = GetComponent<Rigidbody2D>();
 
             speed = 3f;
 
 
             InvokeRepeating("speedUp", 10f, 10f);
 
 
 
 
 
         
 
             p1=0;
 
             p2=0;
 
 
 
     }
 
 
 
     void Update () {
 
             
 
             
 
                 if(rb.velocity == new Vector2(0f,0f)){
 
                     if (Input.GetKeyDown(KeyCode.Space)){
 
                     rb.velocity = new Vector2(2f , 1f ) * speed ;
 
                      }
 
             }
 
         
 
         
 
 
 
     }
 
     
 
     void OnTriggerEnter2D(Collider2D other){
 
         if(other.gameObject.tag == "finish2"){
 
             
 
             
             p2++;
             
             Instantiate(top, new Vector2(-6.4f, -0.2f), Quaternion.identity);
 
             Destroy(this.gameObject);
 
             updateScore2();
                 
         }
 
         if(other.gameObject.tag == "finish"){
             p1++;
 
             
             Instantiate(top, new Vector2(6.32f, -0.2f), Quaternion.identity);
 
             Destroy(this.gameObject);
 
             
 
             updateScore();
         }
     }
     
     void updateScore(){
 
         scoretext.text = p1.ToString();
 
     }
 
     void updateScore2(){
 
         scoretext2.text = p2.ToString();
     }
 
     void speedUp(){
         direct = rb.velocity.normalized;
 
         rb.velocity += direct*2f;
     }
 
 }

`

Both my p1 and p2 (scores) just increases 1 time. But they are under the same condition with

instantiate and everytime one of the players scores, the instantiate works but p1 and/or p2 stay at

the value 1.

Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kalibcrone · Jul 23, 2017 at 06:13 PM

Each time you instantiate the GameObject ball, your Start() method is being called. Being that in your Start method, you have p1 = 0; p2 = 0; you effectively reset the score each time. So, once they score, it goes up by 1, and is printed as a string to scoreText, then set back to 0, and repeat.

Instead of Destroying and instantiating your ball again, you should just disable it, or turn off its mesh (in this case, you will need to disable it's mesh as it seems that this script is attached to the ball itself) and then reset it's values, such as it's speed value and it's position.

EDIT: OR, instead, more simply, in your Start function, instead of setting p1 = 0; p2 = 0; set p1 = Int32.Parse(scoreText1.text); and p2 = Int32.Parse(scoreText2.text); This way instead of the score getting set to 0 each time, it is getting set to the previously recorded score.

Good luck!

Comment
Add comment · 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

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

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Run time counter 1 Answer

Only tagged objects raise counter 1 Answer

Making something happen only for one second during an Update function? 1 Answer

Creating an up and down counter 2 Answers

How to stop a countdown from counting down 1 Answer


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