1

My game has a score. The score is represented by gameobjects. The score increases by 10 on a collision event. These events can not be stopped. I would like for the score to stop increasing on conditional "GameOver".

I am wondering how to stop the score from increasing since the triggering event can not be stopped. Making score = 0 is not good because I want to have the player's end score displayed. I need to somehow disconnect the score from the instantiations at the time of GameOver. Or I need to make the score integer stay the same at the time of GameOver. This is really a conceptual question and I'm not sure how to tackle this problem. Any ideas?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;


public class ScoreHandler : MonoBehaviour {
public int score = 0;
public List<GameObject> destroyList = new List<GameObject>();
public static GameObject[] Score;



// Use this for initialization
void Start () {
    score -= 80;

}

// Update is called once per frame
void Update () {

}



void OnCollisionEnter (Collision col)
{
    if (col.gameObject.name == "carA") {
        score += 10;


    }
    if(col.gameObject.name == "carB")
    {
        score += 10;


    }
    if(col.gameObject.name == "carC")
    {
        score += 10;

    }
    if(col.gameObject.name == "carD")
    {
        score += 10;


    }
    if(col.gameObject.name == "carE")
    {
        score += 10;


    }
    if(col.gameObject.name == "carF")
    {
        score += 10;


    }
    if(col.gameObject.name == "carG")
    {
        score += 10;


    }
    if(col.gameObject.name == "carH")
    {
        score += 10;

    }


    foreach (var go in destroyList)
    {
        Destroy(go);
    }
    destroyList.Clear();


    string scoreText = score.ToString ();


    Score = new GameObject[scoreText.Length];

    for (int i = 0; i < scoreText.Length; i++) {

        Score[i] = (GameObject)Instantiate (Resources.Load (scoreText 
[i].ToString ()));

        Score[i].layer = 8;
        Score[i].transform.localScale = new Vector3 (0.02F, 0.02F, 
0.02F);
        Score[i].transform.localPosition = new Vector3 (0.013F + i * 
0.01F, 0.12F, 0.0F);
        Score[i].transform.Rotate (0, 180, 0);

        destroyList.Add (Score[i]);



    }
  }

}

*This code box has a scroll bar.

3 Answers3

1

If you have GameOver flag, things will get easier.

Suppose you have a flag called when game over occurs:

bool gameOverFlag = false;
.... //something else
void OnGameOver(){
    .... //something else
    gameOverFlag = true;
    .... //something else
}

And, only not to add score (while leaving everything else the same) when game over flag is true on collision event would be quite straight-forward:

if (col.gameObject.name == "carA") {
    score += gameOverFlag ? 0 : 10; //this is where ternary operator will come really handy
    //something else specific for carA, not for score
}

By implementing something like above, only your score won't be changed on collision

Ian
  • 30,182
  • 19
  • 69
  • 107
  • Thanks for the help this worked perfectly. I didn't know this technique before so I learned something new too. Thanks again Ian this helped a lot. –  Jan 02 '16 at 21:38
  • You are welcome. ;) You could search more about ternary operator as well as writing flag. Yes, those are not uncommon. – Ian Jan 03 '16 at 01:43
  • @learncode999 another thing you want to note if your car object all do the same thing, you may want to replace your multiple `if` with single `if` by comparing the first three letters of the `gameObject.name`. You can do it easily with `string.StartsWith`, that is `if ((col.gameObject.name.StartsWith("car"))` //do something. – Ian Jan 03 '16 at 02:58
0

Why are objects colliding if the game is over?

One easy way to keep the game running in the background but stop the interaction is to disable the collider of the player when game is over.

GetComponent<Collider>().enabled = false;

One other solution would be to check if game is running and only add score then.

void OnCollisionEnter (Collision col)
{
    if(!gameRunning)
        return;

    // score logic
}

However, I would recommend that you separate your code parts to be able to control the different states of your game. If you don't want to use a State Machine you could use Enum States. This will make it easier to manage as your game gets more complex.

Mattias
  • 3,907
  • 4
  • 28
  • 50
  • If I disable collisions, my GameObjects fly off of the map because there is nothing to stop them. Setting thrust = 0 is not working for me either. I will think more about it and then comment what I figure out. Thanks for the reply. –  Jan 02 '16 at 04:14
0

Just check if score is < 10 and also I suggest moving all your conditions in one block:

void OnCollisionEnter (Collision col)
{
    if ((col.gameObject.name == "carA" || col.gameObject.name == "carB" || col.gameObject.name == "carC"
      || col.gameObject.name == "carD" || col.gameObject.name == "carE"
      || col.gameObject.name == "carF" || col.gameObject.name == "carG"
      || col.gameObject.name == "carH") && score < 10 ) 
    {
        score += 10;
    }

//Rest of the code
}
Ghasem
  • 14,455
  • 21
  • 138
  • 171