- Home /
Problem with Counting Collisions
Very simply I'm doing the Beginner Challenge 3, and I'm pretty sure I got everything down the only problem is this weird bug I can't figure out. I have it so when my sphere is launched and hits a box my score updates by adding one to score. This all works just fine...the first time.
Afterwards it never updates, and it seems it's only the "Score++;" that doesn't work anymore after the first "hit". I've done Debug.Log messages to see if it's even running through the if logic and it does, it displays every time I hit the cubes again, it just doesn't add one to score. I also checked if it's a cube problem, but the same thing happens on each cube when their first hit.
I've also tried using "Score = Score + 1;", "Score+=1;" to no avail. Lastly, I've checked the GUIText and it can change just fine if I have it display something different from hitting the floor to a cube, so I'm pretty sure it's just the Score++; part that isn't working right.
TL;DR: A Score++; counting variable for counting sphere to cube collisions isn't incrementing after the first hit.
Update: I got around this by creating another script that updates the score by accessing another script on the GUIText. Basically what's done here:http://answers.unity3d.com/questions/200262/changing-variables-in-another-script.html
Code:
var Score : int = 0;
function OnCollisionEnter(hit : Collision) {
var myText : GUIText = gameObject.Find("GUI Text").GetComponent(GUIText);
if(hit.gameObject.name == "cCube")
{
//(I added a Debug.Log message here to check the value of Score, it seems to reset to 0 here everytime whether or not the variable is declared outside or inside the function.)
Score++;
myText.text = "Score: " + Score;
Destroy(gameObject.Find("FireCannon(Clone)"));
}
else if(hit.gameObject.name == "bCube")
{
Score++;
myText.text = "Score: " + Score;
Destroy(gameObject.Find("FireCannon(Clone)"));
}
else if(hit.gameObject.name == "aCube")
{
Score++;
myText.text = "Score: " + Score;
Destroy(gameObject.Find("FireCannon(Clone)"));
}
}
Answer by iwaldrop · Feb 10, 2013 at 06:22 AM
From what I see of you code Score will always equal 1 at the end of OnCollisionEnter. This is because you declare the variable within the method. If you want to use it as a counter between OnCollisionEnter calls then you need to declare it as a member variable; outside of the scope of your method. ;)
Answer by Zerodegrees · Feb 10, 2013 at 05:18 PM
Didn't work, I should have put that I tried that already. I've tried putting the declaration both inside the function and outside at the beginning like normal. This code was just when the last time I had it inside. It also isn't resetting to 0 either, it starts at 0, I hit a block it goes to 1 then any time after that it just stays at 1. I can hit any block and it won't go up or down or anything. I've tried setting the floor to work the same way where it adds 1 when it hits the floor and the same thing happens.
(In response to iwaldrop, still new to the forums)
Your answer
Follow this Question
Related Questions
Coroutine freezes editor but only sometimes. Why ? Why is it happening randomly ? 3 Answers
Unity freezes after running this basic code. 2 Answers
How to call unity event once every time my object reach destination in Update()? 1 Answer
Is there any way to upscale one variable if another one goes up? (C#) 1 Answer
OnCollisionEnter2D is called but the actually collision isn't resolving. 0 Answers