- Home /
Most efficient way to get scores
Ok so here is the scenario... I am creating a game where you throw bags at a board. If the bags land on the board you get a score. If they land on the board but fall off or are knocked off then the score goes down. The area I need help is in the scoring. What is the easiest way to accomplish this? I am using prefabs for the bags if that makes any difference. I am not new to code but I am new to C# and unity.... soooooo yea any help you could provide would be awesome and very appreciated.
Answer by ShadoX · Jul 29, 2014 at 08:11 PM
The easiest way would probably to just have a trigger on the board and another below it. (in which case the bags would have to have a rigidBody attached to them)
And on those triggers just use something like
http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
to increase and decrease the score when a bag hits the trigger. (just make sure that it doesn't keep increasing / decreasing the score over and over again)
i did that using void OnTriggerEnter(Collider col){ //otherObject.GetComponent().canScore = false; otherObject.GetComponent ().canScore = false; bool hit = otherObject.GetComponent().canScore; if (col.gameObject.tag == "bagClone" && hit == false){ int currentScore = int.Parse (score.GetComponent ().text) + 1;
print (otherObject.GetComponent<bagDetect>().canScore);
score.GetComponent<GUIText> ().text = currentScore.ToString ();
otherObject.GetComponent<bagDetect>().canScore = true;
print (otherObject.GetComponent<bagDetect>().canScore);
}
and it did not work... thoughts??
also tried
public GameObject score; //reference to the ScoreText gameobject, set in editor
public GameObject otherObject;
void OnTriggerEnter(Collider col) {
bool hit = otherObject.GetComponent<bagDetect>().canScore;
print (hit);
if (col.gameObject.tag == "bagClone") {
otherObject.GetComponent<bagDetect> ().canScore = false;
if (col.gameObject.tag == "bagClone" && hit == false) {
otherObject.GetComponent<bagDetect> ().canScore = true;
int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + 1; //add 1 to the score
score.GetComponent<GUIText> ().text = currentScore.ToString ();
print (hit);
}
//otherObject.GetComponent<bagDetect> ().canScore = false;
}
and it does not work either... the score is still incremented by more than one sometimes...
Your answer
Follow this Question
Related Questions
Scoring System 1 Answer
Trying to get this coin system to work to no avail 1 Answer
Collider just for Tag (2D) (C#) 1 Answer
Multiple scores from collisions how do I stop after one collsion 3 Answers
Distribute terrain in zones 3 Answers