- Home /
Destroy Object - Score
I try to make script for score using
Cube Destroy GUI Text
private var score : int = 0;
var guiScore : GUIText;
function Start () {
guiScore.text = "Score: 0";
}
function Update () {
}
function OnMouseDown() {
if(Input.GetMouseButton(0)){
Destroy(gameObject);
score += 10;
guiScore.text = "Score: " + score;
print ("asd");
}
}
This i script i have but i have problem when i destroy 1 cube score is go up from 0 to 10 but when i destroy second score is 10. Where is my mistake??
Answer by robertbu · Feb 13, 2013 at 01:32 AM
Your problem is that you are destroying the game object the script is attached to in line 14. So the second time you click the button, the script no longer exists.
Problem is in that i set script on 3 object (Cube) and when i destroy 1 object score is 10 and after i destroy second object score is restart and go to 10 again.
guiScore.text = "Score: 0";
How can i make script which will be set on First Person Controller something like this
var guiScore : GUIText;
function Start (){
guiScore.text = "Score: ";
function Update();
if(click on object with name Cube){
Destroy(Cube)
score += 10;
guiScore.text = "Score: " + score;
}
}
You can handle this one of two ways. First, you can make the score "live" on a game object other than the cube. That way when the cube is destroyed, the score is not also destroyed. You would attach a scoring script to an empty game object and then your cubes would tell the scoring script to deduct points. See:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
The second way is Physics.Raycast() to find your object ins$$anonymous$$d of doing On$$anonymous$$ouseDown(). Here is the start of the raycasting logic taken from the reference:
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, 100)) {
print ("Hit something");
}
Here is a script. Attach it to an empty game object. Put some cubes and spheres in the scene. Run the app and take a look at the Console window.
var score : int = 0;
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
print ("Hit something: "+hit.collider.name);
if (hit.collider.name == "Cube") {
score += 10;
Debug.Log("Scored again! :"+score);
Destroy(hit.collider.gameObject);
}
}
}
}
Hire is script for score.But Problem is in that i see score only 0.5 sec.
When i click on first Cube score from "Score: " go to "Score:10" When i click second Cube from Score: go to "Score:20" after 0.5sec score back to "Score: " and continues.
var score : int = 0;
var guiScore : GUIText;
function Update () {
guiScore.text = "Score: ";
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
var ray : Ray = Camera.main.ScreenPointToRay
(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
print ("Hit something: "+hit.collider.name);
if (hit.collider.name == "Cube") {
score += 10;
guiScore.text = "Score: " + score;
Debug.Log("Scored again! :"+score);
Destroy(hit.collider.gameObject);
}
}
}
}
Answer by jmaster2012 · Feb 13, 2013 at 01:35 AM
I THINK, I am not sure, but I THINK you told it to be equal or above ten at line 15. you should probably put, score +1...
Again I THINK I AM NOT SURE!! DO NOT COMMENT MEAN THINGS IF I AM WRONG
Not even close. $$anonymous$$eep coding though! You'll learn!
Your answer
Follow this Question
Related Questions
How do i link a GUI text to a variable text which is a integer? 2 Answers
score script + gameobject apears for 15 secends 0 Answers
how do i count leftover enemies in screen? 0 Answers
Check GameObject postion 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers