- Home /
How to Subtract the score
Hey guys, I'm making a prototype game for one of my uni module's. Its a simple game where the player has to try and catch the spawning object. I've got the score to increment by 1 but how do I go about taking the score away if the player misses the object.
code using UnityEngine; using System.Collections;
public class Score : MonoBehaviour
{
public GUIText SugarProductionText;
private int SugarProduction;
// Use this for initialization
void Start ()
{
SugarProduction = 0;
SetSugarProductionText();
}
// Update is called once per frame
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Sugar")
{
other.gameObject.SetActive(false);
SugarProduction = SugarProduction + 3;
SetSugarProductionText();
}
else if(other.gameObject.tag == "Terrain")
{
other.gameObject.SetActive(true);
SugarProduction = SugarProduction - 1;
SetSugarProductionText();
}
}
void SetSugarProductionText()
{
SugarProductionText.text = "SugarProduction" + SugarProduction.ToString();
}
}
I tried applying a tag to the terrain, so if the object spawning touches the terrain the player score would go down, but it doesn't.
Answer by tanoshimi · Nov 29, 2013 at 04:37 PM
Is this script attached to the player? Because your two collision tests currently look for collision between this object and "Sugar" and with "Terrain"... (i.e. it does not test anywhere whether "Sugar" collides with "Terrain", which I think is what you're describing).
It sounds like your script would be better attached to the sugar object, and have conditions in OnCollisionEnter to test for collisions with "Player" and with "Terrain".
Your answer
Follow this Question
Related Questions
Moving Player After Falling Certain Distance [Beginner/C#] 1 Answer
Is it possible to convert a Collider into a Spawner? 0 Answers
Spawn OnTriggerEnter 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers