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
0
Question by Unity3DJava · Mar 11, 2013 at 05:12 AM · javascriptscoreadd

script woe adding score

//My Code var playerScore : int = 0; var writing : GUIText;

function OnCollisionEnter(collision : Collision) { if(collision.gameObject){

Destroy(this.gameObject); playerScore++;

writing.text = "Score: " + playerScore;

} }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kleptomaniac · Mar 11, 2013 at 05:35 AM

The problem is that when you run you OnCollisionEnter() function, you call Destroy(this.gameObject) before you are even able to increment your score. This means it will not work as this script is attached as a component of your object instance and is destroyed along with your object. I would suggest either changing you execution order so that it increments before the Destroy() method call, or else controlling your score in another script altogether (I would go with the latter).

Hope that helps,

Klep

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 Unity3DJava · Mar 12, 2013 at 12:13 AM 0
Share

Changing the execution order did not work :( So I tried putting it in its own script. In the if statement it wont work, is there another way i could add score by destroying the enemy?

Code: var playerScore : int = 0; var writing : GUIText; var enemy : GameObject;

function Update() { if(Destroy.enemy) { playerScore+=100;

writing.text = "Score: " + playerScore; }

}

avatar image Unity3DJava · Mar 12, 2013 at 11:47 PM 0
Share

Guess no one can answer my question. :l

avatar image Kleptomaniac · Mar 13, 2013 at 07:34 AM 0
Share

Ins$$anonymous$$d of putting this script on each individual enemy, why not put it on your player object? Like so:

 private var playerScore : int = 0;
 var writing : GUIText;
 var enemy : GameObject;
 
 function OnCollisionEnter (collision : Collision) {
      if (collision.gameObject.CompareTag("Enemy") {
           Destroy(collision.gameObject);
 
           playerScore += 100;
           writing.text = "Score: " + playerScore;
      }
 }

Simply tag your enemy "Enemy" and you're good to go. :)

$$anonymous$$lep

avatar image Unity3DJava · Mar 13, 2013 at 09:00 PM 0
Share

I followed your steps and unfortunately did not work. ;( I assigned the tag to the enemy and I also the writing as the gui text.

avatar image Kleptomaniac · Mar 14, 2013 at 05:12 AM 0
Share

Did you get any errors or did nothing happen? I need more information in order to help you.

Edit: Also, I noticed an error in my script. You don't need the var enemy : GameObject line. This still shouldn't cause it to not work though.

Show more comments

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

11 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

Related Questions

Adding a value to a variable every second? 1 Answer

I am creating a pause button and when I click it the score keeps rising. How can I stop it? 1 Answer

A fancier method to add numbers 1 Answer

Add to score every second 3 Answers

Keeping track of items 1 Answer


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