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;
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:
public class GameManager : MonoBehaviour {
private static GameManager _instance; //This will only ever be created once
//Add whatever game-related variables you need here
public int Score;
public static bool IsActive {
get {
return this._instance != null;
}
}
private static GameManager Instance {
get {
if (this._instance == null) {
//Check if it exists but just isn't connected yet
this._instance = Object.FindObjectOfType(typeof(GameManager)) as GameManager;
if (this._instance == null) {
//Create the GameManager ONLY the first time
GameObject g = new GameObject("gameManager");
DontDestroyOnLoad(g); //This object will persist through loading different scenes
this._instance = g.AddComponent<GameManager>();
}
}
return this._instance;
}
}
}
Why are you destroying this.gameObject? Is is actually destroying the object of which this script is attached to, therefore deleting the functionality?