Search Unity

Destroying objects ( one click mouse button) and adding scores.

Discussion in 'Scripting' started by sonys222, Jul 9, 2015.

  1. sonys222

    sonys222

    Joined:
    Jun 3, 2015
    Posts:
    32
    Hey. I'm a new in unity and i have a problem with destroying a clone objects from spawner. New objects in the game to set up and can be destroyed, but the points are added only once after the first click. Please help me.


    using UnityEngine;
    using System.Collections;

    public class DestroyObject : MonoBehaviour
    {
    public int myScore = 0;

    //public GameObject other;
    public TextMesh scoreText;
    private int score;

    void OnMouseDown()
    {
    // this object was clicked - do something
    Destroy (this.gameObject);
    myScore = myScore + 1;
    scoreText.text = "Score: " + myScore;

    }
    }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you are destroying the game object, and everything on it... so you click, score is added, pooft, gone

    have the score on something else, have this script tell that script "add a point" and then have it destroy itself
     
  3. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Create a game object called something like "GameManager", which will hold information about your game, such as the score. Then create a script that looks like this:

    Code (CSharp):
    1. public class GameManager : MonoBehaviour {
    2.     private static GameManager _instance; //This will only ever be created once
    3.    
    4.     //Add whatever game-related variables you need here
    5.     public int Score;
    6.  
    7.     public static bool IsActive {
    8.         get {
    9.             return this._instance != null;
    10.         }
    11.     }
    12.  
    13.     private static GameManager Instance {
    14.         get {
    15.             if (this._instance == null) {
    16.                 //Check if it exists but just isn't connected yet
    17.                 this._instance = Object.FindObjectOfType(typeof(GameManager)) as GameManager;
    18.                 if (this._instance == null) {
    19.                      //Create the GameManager ONLY the first time
    20.                      GameObject g = new GameObject("gameManager");
    21.                      DontDestroyOnLoad(g); //This object will persist through loading different scenes
    22.                      this._instance = g.AddComponent<GameManager>();
    23.                 }
    24.             }
    25.             return this._instance;
    26.         }
    27.     }
    28. }
     
  4. drew_massey

    drew_massey

    Joined:
    Apr 28, 2013
    Posts:
    94

    Why are you destroying this.gameObject? Is is actually destroying the object of which this script is attached to, therefore deleting the functionality?
     
  5. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Yes, the way he has it set up, the score gets deleted along with the object.