- Home /
Multiple scores from collisions how do I stop after one collsion
I am creating a game that you throw objects at a board and if the object lands on the board you get a score. The problem is when the object hits the board sometimes it bounces or rolls and multiple scores are logged. I would like for only one score to be annotated per throw. Thanks in advance and sorry if this question has been asked before... I looked and could not really find anything similar. Here is the code:`using UnityEngine; using System.Collections;
public class ID355 : MonoBehaviour { public GameObject score; //reference to the ScoreText gameobject, set in editor public GameObject otherObject;
void OnCollisionEnter() //if bag hits board
{
otherObject.GetComponent<bagDetect>().bagHit = false;
bool hit = otherObject.GetComponent<bagDetect>().bagHit;
if (hit == false)
{
int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + 1; //add 1 to the score
score.GetComponent<GUIText> ().text = currentScore.ToString ();
otherObject.GetComponent<bagDetect>().bagHit = true;
print(hit);
}
}
void OnTriggerEnter() //if bag hits collider
{
int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + 3; //add 3 to the score
score.GetComponent<GUIText> ().text = currentScore.ToString ();
}
}
` and here is the code for otherObject:
using UnityEngine;
using System.Collections;
public class bagDetect : MonoBehaviour {
public bool bagHit = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
$$anonymous$$ake some kind of oneshot boolean to switch off the scoring after the first one, or add a time delay before any scoring can take place after a score has been logged.
that is kinda what I tried doing but the problem is that there will be four objects thrown and each of the objects is a prefab and when I do it that way it scores the first one but no others after it... so I don't know what the best course of action for a solution would be... maybe make four separate objects ins$$anonymous$$d of prefab? or an array of some sort
Answer by 432qwerty · Jul 28, 2014 at 04:47 PM
You can either:
Have a one-shot boolean that stops the score retriggering, as @meat5000 suggested
Use void OnCollisionStay() instead of void OnCollisionEnter() if you have this problem every time a bag hits the board.
I would say the one-shot bool is probably your best option.
Answer by DiGiaCom-Tech · Jul 28, 2014 at 06:02 PM
Your prefabs become unique objects when instantiated. So you need a flag in the object being thrown prefab code like 'IsScored' initially set to false.
Then on collision with the board you check the state of the flag to see if the object was scored. It not scored (e.g. if (!IsScored) then...) then increment the score and set the objects 'IsScored' to true. Subsequent collisions of the thrown object will not increment the score.
Note, this scores the first time the object hits the board. So, if it hits the ground (or something else) first and then hits the board is not a score, then you'll need to set it on any collision vs. just the board collision.
Answer by BaldiWonKenobi · Jul 28, 2014 at 06:50 PM
DiaGiaCom," Your prefabs become unique objects when instantiated. So you need a flag in the object being thrown prefab code like 'IsScored' initially set to false.
Then on collision with the board you check the state of the flag to see if the object was scored. It not scored (e.g. if (!IsScored) then...) then increment the score and set the objects 'IsScored' to true. Subsequent collisions of the thrown object will not increment the score."
this is what I did... with the otherObject (otherObject is the code on the prefab)and the first code listed... I understand what you are saying I think I am missing something small with this ....
So maybe something like this? void OnCollisionEnter() //if bag hits board { bool currentScore = true; if (currentScore == true) { int.Parse (score.GetComponent ().text) + 1; //add 1 to the score score.GetComponent ().text = currentScore.ToString (); } }
I tried this:
public GameObject score; //reference to the ScoreText gameobject, set in editor
public GameObject otherObject;
void OnCollisionEnter() //if ball hits board
{
otherObject.GetComponent<bagDetect>().bagHit = false;
//bool hit = otherObject.GetComponent<bagDetect>().bagHit;
if (otherObject.GetComponent<bagDetect>().bagHit != true)
{
int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + 1; //add 1 to the score
score.GetComponent<GUIText> ().text = currentScore.ToString ();
otherObject.GetComponent<bagDetect>().bagHit = true;
print(otherObject.GetComponent<bagDetect>().bagHit);
}
}
but it did not work either even though the boolean on otherObject was changed to true? any thoughts?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
how to keep track of bricks broken 1 Answer
Most efficient way to get scores 1 Answer
Multiple Cars not working 1 Answer