- Home /
Scoring not working
My ball hits the right wall it gives the point to player 2 and if it hits the left wall it also give the point to player 2.
This is the code for it:
#pragma strict
static var playerScore01 : int = 0;
static var playerScore02 : int = 0;
var theSkin : GUISkin;
static function Score (wallName : String) {
if (wallName == "rightWall")
{
playerScore01 += 1;
}
else {
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-12, 25, 100, 100), "" + playerScore01);
GUI.Label (new Rect (Screen.width/2+150-12, 25, 100, 100), "" + playerScore02);
}
By the sound of things wallName always equals "rightWall", your parent function might not be forwarding the correct information. Can you Debug.Log wallName before the if statement and see what that outputs?
Thanks for the idea tayloredgames. I have tried that but both come up as being player 2's score. As you said, it seems to always equel rightWall but I can't figure out why. The code up there is Game$$anonymous$$anager.js and bellow I have SideWalls.js. SideWall is for detecting the ball hitting the side and Game$$anonymous$$anager is for allocating scores.
#pragma strict
function OnTriggerEnter2D (hitInfo : Collider2D) {
if (hitInfo.name == "Ball")
{
var wallName = transform.name;
Game$$anonymous$$anager.Score (wallName);
hitInfo.gameObject.Send$$anonymous$$essage ("ResetBall");
}
}
I also have the code from GameSetup.js. It is responsible for setting up the hit boxes for the aspect ratio through a trigger event.
#pragma strict
//Reference the camera
var mainCam : Camera;
//Reference the colliders we are going to adjust
var topWall : BoxCollider2D;
var bottomWall : BoxCollider2D;
var leftWall : BoxCollider2D;
var rightWall : BoxCollider2D;
//Reference the players
var Player01 : Transform;
var Player02 : Transform;
function Update () { //Only set this to Update if you know the screen size can change during a playsession.
//$$anonymous$$ove each wall to 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 * 2, 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);
//$$anonymous$$ove the players to a fixed distance from the edges of the screen:
Player01.position.x = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)).x;
Player02.position.x = mainCam.ScreenToWorldPoint (new Vector3 (Screen.width -75f, 0f, 0f)).x;
}
Answer by Konbex · Feb 09, 2014 at 10:27 PM
Try using two if statements instead of a single if/else.
Also, sorry for the stupid question but have you checked to make sure the walls have the correct names?
Thank you $$anonymous$$onbex, that was the problem. I had the left wall in both the right wall and left wall slot. It is working now.
Answer by MaGuSware™ · Feb 09, 2014 at 11:48 PM
The variable names do not equal the object name.
What you are doing is trying to compare the object name of the collided wall with a string version of the variable name, not with what the wall is actually called.
I mean, I highly doubt the walls in your object hierarchy are called "rightWall" and "leftWall", right?
In your object hierarchy, call your walls something like "Left Wall" and "Right Wall" if you haven't already, then change your if/else statement to this:
if(wallName == "Right Wall")
{
playerScore01 += 1;
}
else if (wallName == "Left Wall")
{
playerScore02 += 1;
}
They were and it was solved. I put leftWall in the place of rightWall on the GameSetup.