- Home /
Destroy a game object if score = 1000 ?
Hello . I have a question guys . I got a score script written in C# and the Class is " Score " . And i have game object that i want to destroy it if the Score = 1000 . How can i do that please ?
public Score Point;
public int score;
// Use this for initialization
void Start () {
if( Point= 10){
Destroy(gameObject);
}
}
Thanks in advance!
Answer by clunk47 · Nov 15, 2013 at 09:14 PM
This is pure EXAMPLE, should get you where you need to be. Say you have a GameObject called "Scripts" in your scene's Hierarchy. This GO has a script attached, called "Score". Let's say this is "Score".
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour
{
[HideInInspector]
public int score = 0;
void Update()
{
//TEST
if(Input.GetKey(KeyCode.Return))
score+=10;
}
void OnGUI()
{
GUILayout.Label ("Score - " + score.ToString ());
}
}
Then, say you have a GO with a script attached called "DestroyOnWin", and this is "DestroyOnWin" class:
using UnityEngine;
using System.Collections;
public class DestroyOnWin : MonoBehaviour
{
public Score scoreScript;
void Start()
{
//Find Scripts GameObject, and get Score Component.
scoreScript = GameObject.Find ("Scripts").GetComponent<Score>();
}
void Update()
{
if(scoreScript != null && scoreScript.score >= 1000)
Destroy (gameObject);
}
}
worked great ! Thank a lot sir:). I got another problem :( . It's about playing sound on destroy . I'm using audio.PlayOneShot the sound play with unbelievable lag ! It's play likes it's on slow motion ! :( any idea sir ?
If this helps, please remember to accept the answer and vote up any other comments or answers that were helpful. Please refrain from asking new questions in comments to answers or other questions, just as future reference, ask as a separate question in UA.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Why does this do nothing (Health Script) 2 Answers
What is wrong with my script? 1 Answer
How to destroy and trigger on enter. 1 Answer