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 hellgas · Jul 03, 2014 at 03:50 AM · javascriptscorepongpaddle

Scoring not working for pong

on either side of the walls the score always goes to Player02 i have tried everything to change it but nothing works

this is the code below i have tried many methods especially with the PlayerScore01 += 1

 #pragma strict
 
 static var playerScore01 : int = 0;
 
 static var playerScore02 : int = 0;
 
 var theSkin : GUISkin;
 
 static function Score (wallName : String) {
 
     if (wallName == "Right Wall")
     {
         playerScore01 += 1;
     }
     else if (wallName == "Left Wall")
         playerScore02 += 1;
     }
     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, 25, 100, 100), "" + playerScore01);
     GUI.Label (new Rect (Screen.width/2+150, 25, 100, 100), "" + playerScore02);
 }








Comment
Add comment · Show 4
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 NoseKills · Jul 03, 2014 at 04:49 AM 1
Share

So apparently you are always calling Score() with parameter "Left Wall". Thats all we can say after seeing this script.

avatar image hellgas · Jul 03, 2014 at 12:49 PM 0
Share

I do not know what else to do iv tried so many methods my brain hurts from trying to think of other ways to do this.

avatar image Josh Naylor ♦♦ · Jul 03, 2014 at 01:17 PM 0
Share

Use the OnCollisionEnter method and then check the game objects name.

http://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter

avatar image hellgas · Jul 03, 2014 at 04:17 PM 0
Share

i have done that for the walls #pragma strict

 function OnTriggerEnter2D (hitInfo : Collider2D) {
     if (hitInfo.name == "Ball")
     {
         var wallName = transform.name;
         Game$$anonymous$$anager.Score (wallName);
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Entwicklerpages · Jul 03, 2014 at 06:00 PM

First, is your wall correctly named? And is the trigger at the correct position? Maybe you have duplicated the wall for player1 and forgot to move it to the right location.

Also, I think you don't should use the name of the gameobject. I think using a bool or int is a better choice. And in the trigger script get the right value with a public var. (You can change the value of a public variable in the inspector).

 public var player : int = 1;
 
 function OnTriggerEnter2D (hitInfo : Collider2D) {
     if (hitInfo.name == "Ball")
     {
         GameManager.Score (player);
     }
 }

And in the GameManger script change the parameter from String to int.

 static function Score (plrNo : int) {
  
     if (plrNo == 1)
     {
         playerScore01 += 1;
     } else if (plrNo == 2) {
         playerScore02 += 1;
     }
     Debug.Log("Player Score 1 is " + playerScore01);
     Debug.Log("Player Score 2 is " + playerScore02);
 }

PS: You have forgotten the closing bracket in the Score function. Look at my script. You have also forgotten the opening bracket for the else if

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 hellgas · Jul 06, 2014 at 11:11 PM 0
Share

Now the score is only going to player 1

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

In my pong game How do I prevent paddle from going threw walls? 1 Answer

Help with score 1 Answer

Score Help!!!! 2 Answers

Score Keeper / Kill Counter Help 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