- Home /
Simple collision function acting strangely
On collision, score increases by 10. I used Debug.Log in the collision function first to make sure everything worked and it worked perfectly. Now the score starts at 20, and on collision one third of the time adds 20, one third of the time subtracts 20, and the other third of the time it does not change. I must be making a mistake in this code somewhere because like I said, I used debug to check that my objects were set up right. I appreciate any advice.
public int score = 0;
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Plane")
{
//On collision, add 10 to score, access text, change text to score.
score += 10;
TextMesh ScoreMesh = GameObject.Find("Score").GetComponent<TextMesh>();
ScoreMesh.text = score.ToString ();
}
}
Answer by allenallenallen · Dec 23, 2015 at 06:54 AM
Everything seems okay. Do you have other scripts accessing the TextMesh that's showing the scores? Perhaps another script is overwriting what this scripts writes to the score.
Thanks for the help. Unity moderators had to look over my post for hours before submitting it so by the time this got posted my friends at Stack Overflow and I solved it. It was a beast. Anyways thanks for the help again.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Object following a path and colliding with other objects with physics. 1 Answer
Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer
I am not able to call a function from OntriggerEnter event :( 2 Answers