- Home /
Multiple objects being destroyed, count for them being destroyed doesnt go up past once.
I have a script that's on a prefab, the script finds a text component to attach it to cause prefabs can't remember things like that. sets the "score" to 0 because its a score counter and then updates the text but whenever an object is destroyed with the script on it, it should add 100 points but instead as my poorly typed title says, it only counts up by 100 once, and then never again. the script is attached to the object that is destroyed, but its attached to each object so I don't understand what ive done wrong
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Destroy : MonoBehaviour {
public Text scoreText;
private int score;
// Use this for initialization
void Start () {
scoreText = GameObject.FindGameObjectWithTag("Score").GetComponent<Text>();
score = 0;
scoreText.text = "Score: " + score.ToString ();
}
// Update is called once per frame
void Update () {
}
void OnMouseDown ()
{
Destroy (gameObject);
score = score + 100;
scoreText.text = "Score: " + score.ToString ();
}
}
Answer by KittenSnipes · Sep 25, 2018 at 08:06 AM
@Vanna5556 The other answer is indeed correct and if you do any of the Unity tutorials that include a score then you would have your answer but for the sake of giving an answer I will give some scripts with comments.
Oh and some simple instructions:
1) Add the first script to an object that will not be destroyed under any circumstances during gameplay
2) Any object that will be destroyed should have the second script on it and a reference to the object holding the first script so when it is destroyed it can add points to our score.
The first script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class ScoreOnDestroy : MonoBehaviour {
//Lets get our reference to the text object
public Text scoreText;
//Our score variable
private int score;
// Use this for initialization
void Start () {
//Make sure our score is 0 when the game starts
score = 0;
//Didplay our current score
scoreText.text = "Score: " + score;
}
//Function to add points to our current score and display it
public void AddToScore(int amountToAdd)
score = score + amountToAdd;
scoreText.text = "Score: " + score;
}
}
The second script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Destroy : MonoBehaviour {
//Public variable to get the object with the script holding our score
public ScoreOnDestroy score;
//Value the object is worth when it is destroyed
public int pointsToAdd = 100;
// Use this for initialization
void Start () {
//If the score holder was not set then try and grab it from the parent of the script
if (score == null) {
score = GetComponent<ScoreOnDestroy>();
}
}
void OnMouseDown ()
{
score.AddToScore(pointsToAdd);
Destroy (gameObject);
}
}
After a small bit of editing I got your instructions to work fine, as long as I manually referenced the object the first script is set on. I don't know what I have to do to reference it automatically because trying to make score or ScoreOnDestroy the game object, it gave an error And i see in the script it references ScoreOnDestroy as a component which is.. impossible because its a seperate game object.
First script
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class ScoreOnDestroy : $$anonymous$$onoBehaviour {
//Lets get our reference to the text object
public Text scoreText;
//Our score variable
private int score;
// Use this for initialization
void Start () {
//$$anonymous$$ake sure our score is 0 when the game starts
score = 0;
scoreText = GameObject.FindGameObjectWithTag("Score").GetComponent<Text>();
//Display our current score
scoreText.text = "Score: " + score;
}
//Function to add points to our current score and display it
public void AddToScore(int amountToAdd) {
score = score + amountToAdd;
scoreText.text = "Score: " + score.ToString();
}
}
Second Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Destroy : $$anonymous$$onoBehaviour {
//Public variable to get the object with the script holding our score
public ScoreOnDestroy score;
//Value the object is worth when it is destroyed
public int pointsToAdd = 100;
// Use this for initialization
void Start () {
//If the score holder was not set then try and grab it from the parent of the script
if (score == null) {
score = GetComponent<ScoreOnDestroy>();
}
}
void On$$anonymous$$ouseDown ()
{
score.AddToScore(pointsToAdd);
Destroy (gameObject);
}
}
@Vanna5556 If you want to find it automatically as you say you can tag it or loop through each object in the scene and set score as the object that contains the first script.
object[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject));
foreach (object o in obj)
{
GameObject g = (GameObject) o;
if (g.GetComponent<ScoreOnDestroy>() != null) {
score = g.GetComponent<ScoreOnDestroy>()
}
}
Thank you so much for your help! It works now, helped me learn alot.
Answer by tormentoarmagedoom · Sep 25, 2018 at 07:45 AM
Good day,
You are storing a different points variable in each object, so each object is storing 100 points before beeing destroyed, but you dont have a "general" points counter...
You need to have an external general script that store all the points...