Problem with a simple score keeping system.

I have a scene with a building made up of cubes and fixed joints connecting them all together:

I applied this script to each individual piece of my scene expecting it to +1 to my currentScore var when it collided with “floor”.

static var currentScore : int = 0;

    function OnCollisionEnter(Collision : UnityEngine.Collision){

        if(Collision.gameObject.tag == "floor"){

            currentScore++;
        }
    }

To display my currentScore I created an empty game object on which I applied this ScoreKeeper script and a GUIText component to.

var currentScore : int = 0;
currentScore.ToString();
function OnGUI () {
    GUI.Label (Rect (10, 10, 100, 20), currentScore.ToString());
}
guiText.text = currentScore.ToString();

I’m almost positive that the ScoreKeeper script is to blame but I don’t know what’s wrong.

To my best of knowledge this should work but I’m a very novice coder. Any help would be appreciated! <3

By the way I’m not getting any errors upon playing of the scene. The score just sits at 0.

im not sure about the collision detection there. you could put a line like this inside the if loop in the building piece script:

Debug.Log (“Adding Point”);

to know if that actually works.

The second script looks very messy and i’m not sure wht convert it to string.

since you have currentScore variable allready as a static var in the other script you can simply do this code

say your first script is named “Part”, then:

function OnGUI () {
    GUI.Box (Rect (10, 10, 150, 30),"Score: " +Part.currentScore);
}

As for the collision i’m not quite sure. for starters you don’t need to put “UnityEngine” just put something like “function OnCollisionEnter (AnyName : Collision)”

then do the tag check like this:

if (AnyName.CompareTag (“floor”)) currentScore++;

(no need for the { } when there’s only one sentece in the loop)

If with this it doesn’t work, then you probably need to read more about collisions and triggers to see wich one actually works best for your case.

EDIT: i would try with OnTriggerEnter instead.

Thank you LoneWolf, you lead me down the right road. My new collision detection script uses OnTriggerEnter instead of OnCollisionEnter.

Collision Code:

static var currentScore : int = 0;

    function OnTriggerEnter(collisionInfo : Collider){

       if (collisionInfo.CompareTag ("floor"));
			
			currentScore++;
	   
	}

And the GUI Display code:

function OnGUI () { 
    GUI.Box (Rect (10, 10, 150, 30),"Score: " + CollisionWithFloorScoreUpdate.currentScore); 
}

Thanks again! :]

PS My fps in the scene went up by about 70 using OnTriggerEnter! O_o

Np :slight_smile: i got (and still get) so much help from the forums, the least i can do is trying to help others.

EDIT: Does it work even with the “;” simbol after the if statement?? it shoudn’t be there, but if it works…

[/quote]

Yeah it actually did haha. Didn’t even notice that.

I’ve run into another block. This script for the parts of the building colliding into “floor” works great.

static var currentScore : int = 0;

    function OnTriggerEnter(collisionInfo : Collider){

       if (collisionInfo.CompareTag ("floor"))
			
			currentScore++;
			
		Destroy (GetComponent (CollisionWithFloorScoreUpdate));
	   
	}

But this one doesn’t?

static var currentScore : int = 0;

    function OnTriggerEnter(collisionInfoGold : Collider){

       if (collisionInfoGold.CompareTag ("floor"))
			
			currentScore += 150;
			
		Destroy (GetComponent (CollisionWithFloorScoreUpdate));
	   
	}

Obviously it’s the currentScore += 150;

Is there a different way to write it?

Never mind I got it.

var valueOfObject = 2;
static var currentScore : int = 0;

    function OnTriggerEnter(collisionInfo : Collider){

       if (collisionInfo.CompareTag ("floor"))
			
			currentScore += valueOfObject;
			
		Destroy (GetComponent (CollisionWithFloorScoreUpdate));
	   
	}

I just passed in my own variable for the value of each box hitting the ground.

Playable demo → http://www.creativecodingstudios.com/Unity/74.98.94.199/Break.html

Thank you CreativeCoding for the hosting!

@http://forum.unity3d.com/viewtopic.php?p=349104

Really good start! i like it!

Things i would do:

  • Disable mouse look while on the menu.

  • Move the camera a bit more far and use a lower angle of view instead.

  • Disable the iming reticle when not playing.

  • On level complete “pause the physics” you can make all rigidbodies kinematic to do that. or set time stamp to 0.

  • make the menu accecible again pressing a key and show cursor again. i cundn’t exit the game hehe

How would i aprouch the dev of this game:

  • Make the player race against a clock.

  • Give player power ups. You might find Detonator usefull here. Google for “Unity3d Detonator”.

Again very nice start!

I played the demo. really fun. I just have one question. Are you removing the rigidbody component when they hit the ground and they are done moving? It was lagging way hard on my computer and I was wondering if it was due to the processes of keeping track of the physics applied to everything.

i can answer that, Yes, physics prossesing is very hard on CPU but if a rigidbody is not moving for a period of time it goes to “sleep”.