- Home /
Scoring using the edge collider 2D help!
I'm not that great with coding but I need help with the scoring in my game. Its a simple sandwich stacking 2D game where the players have to collect food ingredients onto the piece of bread. I'm using an edge collider for scoring but the problem is that an ingredient that is already on the sandwich will give the player more points if it moves around and touched the trigger on the collider again. I don't want this. How do I add in my coding "if and ingredient falls on the bread slice then the player receives only one point." Here is what my coding is so far.
using UnityEngine; using System.Collections;
public class Score : MonoBehaviour {
public GUIText scoreText;
public int foodValue;
private int score;
// Use this for initialization
void Start () {
score = 0;
UpdateScore ();
}
void OnTriggerEnter2D () {
score += foodValue;
UpdateScore ();
}
void UpdateScore () {
scoreText.text = "Score:\n" + score;
}
}
void OnTriggerEnter2D (Collision2D other) { score +=foodValue UpdateScore(); }
give this a go, not sure if it will work ill try it to.
Answer by oranmooney · Jul 07, 2017 at 04:28 AM
{
void Update ()
{
scoreText.text = foodValue.ToString();
}
and
score = score + 1;
@wadfgh not really sure if this will work but you have not got an answer yet so.
Answer by Creatom_Games · Jan 05, 2018 at 02:45 AM
Make a variable which checks if the food has already landed on the sandwich. Then, check it int the TriggerEnter2d function.
bool Landed = false;
void OnTriggerEnter2D (){
if(!Landed){
score += foodValue;
UpdateScore ();
Landed = true;
}
}
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animation Moving Sprite Away from Collider 0 Answers
Swapping 2 objects positions when one is dragged into the other 1 Answer
If I deactivate the collider and then reactivate it, the trigger no longer works? 1 Answer
How to get sound to play on certain condition (javascript) 2 Answers