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

People who like this

0 Show 0
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

  • Sort: 
avatar image
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

People who like this

0 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

Instead 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. :)

Klep

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

How to display previous players scores as well as current player score at the end of the game? 0 Answers

Help with score 1 Answer

how do i attach GUITEXT to a prefab 1 Answer

Have readout of score at certain increments (JavaScript) 1 Answer

How to add a score 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