- Home /
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);
}
So apparently you are always calling Score() with parameter "Left Wall". Thats all we can say after seeing this script.
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.
Use the OnCollisionEnter method and then check the game objects name.
http://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter
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);
}
}
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
Your answer
Follow this Question
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