Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by extrime · Feb 09, 2013 at 11:52 PM · destroyscoregui text

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??

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image extrime · Feb 13, 2013 at 09:32 AM 0
Share

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;

}

}

avatar image robertbu · Feb 13, 2013 at 03:18 PM 0
Share

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");
 }

 


avatar image extrime · Feb 13, 2013 at 09:19 PM 0
Share

I am sorry but i don`t understand.

avatar image robertbu · Feb 14, 2013 at 12:21 AM 0
Share

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);
                 }
         
         }
     }
 }
avatar image extrime · Feb 14, 2013 at 10:53 AM 0
Share

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);

}

 }
     }

}

Show more comments
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flaviusxvii · Feb 13, 2013 at 05:25 AM 0
Share

Not even close. $$anonymous$$eep coding though! You'll learn!

avatar image EHogger · Feb 13, 2013 at 03:57 PM 0
Share

jmaster,

 score += 10

is the same as:

 score = score + 10

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges