Counter Issues HELP PLEASE

hey guys,

I have been having problems with a gui counter script i made,

var Counter : int = 0;
var myCollision : Collision;
function OnCollisionEnter () {
 if(myCollision.gameObject.name == "Cube"){
			Counter++;
		guiText.text = "Score : " +Counter; 
 }
}

I have attached this code to the projectile hitting the Cube.

Ideally, the GuiText would be updated every time the cube got hit.

Thanks,

Nick

You could either destroy the cube you hit, thus preventing multiple points off of the same cube.

var Counter : int = 0;
function OnCollisionEnter (myCollision: Collision) {
	if(myCollision.gameObject.name == "Cube"){
		Counter++;
		guiText.text = "Score : " +Counter;
		Destroy(myCollision.gameObject);
	}
}

Or keep track of the last cube you hit so that you don’t count a hit twice.

var Counter : int = 0;
private var lastCube : GameObject;
function OnCollisionEnter (myCollision : Collision) {
	if(myCollision.gameObject.name == "Cube"  myCollision.gameObject != lastCube){
		Counter++;
		guiText.text = "Score : " +Counter;
		lastCube=myCollision.gameObject;
	}
}

In both cases, you would make sure that the name of the object was “Cube” or you would not get points off of it.

Thank you for the help, the only problem i have is that its not updating the score, i get this error “Object reference not set to an instance of an object” on line 5.

HAH, totally missed it… Check your documentation for OnCollisionEnter…

It passes an object (Collision). You dont have that in your script

I updated my code to show the correction

thank you very much, although I have another problem, I feel this is a very dumb question i seem to get this error when i play my game, " There is no ‘GUIText’ attached to the “Projectile” game object, but a script is trying to access it." I have tried to mess around with attaching the gui text to the projectile in the hierarchy but it doesn’t seem to work.

GUIText is a component, click on your object, Components/Rendering/GUIText

That sorta worked, i get no errors, however, the score doesn’t update.

are all the objects you are hitting named Cube? Remember Instantiated objects have (Clone) attached to the end.

None of the Cubes are clones however the projectiles are.