• 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 shelin · May 23, 2011 at 09:53 AM · score

Score Questions

Destruction of the enemy is judged. Score increase. How to write code?

 static var score:int=0;
 var stringToEdit: String;
 /*
 function Start () {
     blood = PlayerPrefs.GetInt("score",3);
     DontDestroyOnLoad(this);
 }
 */
 
 function  OnControllerColliderHit (hit : ControllerColliderHit )
 {
      if(hit.gameObject.tag  == "enemy")
  {
         score+=100;
     }    
 }
 function OnGUI(){
     StringToEdit=score.ToString();
     StringToEdit=GUI.TextField(Rect(450,40,120,35),StringToEdit,50);
     PlayerPrefs.SetInt("Player Score",score);
 }
Comment
Add comment · Show 7
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 Kacer · May 23, 2011 at 10:00 AM 0
Share

what exactly is your problem*, this seems to work fine, from a first glance anyway :)

avatar image demize2010 · May 23, 2011 at 10:03 AM 0
Share

Yep certainly seems ok...

avatar image Mnescat · May 23, 2011 at 11:55 AM 2
Share

How to write code?

Why that's an easy one! With a keyboard!

No but really? what is the question?

avatar image shelin · May 24, 2011 at 03:14 AM 0
Share

Score didn't increase.

avatar image logty · May 24, 2011 at 05:11 AM 0
Share

$$anonymous$$aybe because score is static?? Try taking out the static because I am pretty sure static => no change

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CHPedersen · May 24, 2011 at 07:38 AM

If score didn't increase, it's either because OnControllerColliderHit is never called, or it's because the if-sentence inside it never evaluates to true:

 function  OnControllerColliderHit (hit : ControllerColliderHit ) <--- Is never called 
 {
      if(hit.gameObject.tag  == "enemy") <--- Or is never true
  {
         score+=100;
     }   
 }

Check that the method is actually called by inserting some Debug.Log() in it. Then check the tags on the gameObjects you expect to collide with. Are they "enemy"? Remember that string comparison is case sensitive. Should it be "Enemy"?

Also, your indentation is a little funky. Try to make sure your brackets line up in the code, so it's clear exactly when a block begins and ends.

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 shelin · May 24, 2011 at 08:57 AM 0
Share

static var score:int=0; var stringToEdit: String; / function Start () { blood = PlayerPrefs.GetInt("score",3); DontDestroyOnLoad(this); } /

function OnCollisionEnter(theCollision : Collision) { if (theCollision.gameObject.name == "enemy"){ score += 100; } } function OnGUI(){ StringToEdit=score.ToString(); StringToEdit=GUI.TextField(Rect(450,40,120,35),StringToEdit,50); PlayerPrefs.SetInt("Player Score",score); }

function Awake(){ DontDestroyOnLoad(this);

}

/*function OnApplicationQuit(){

}*/

I change my script. I want to destroy enemy then score inscore . I don't want to collision enemy then score inscore .

avatar image CHPedersen · May 24, 2011 at 09:18 AM 0
Share

To post new code, edit your original question with the changes. Comments do not format code very nicely, no one is going to bother trying to read non-formatted copy-pasted code.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How can I make triggering multiple triggers give more points? 1 Answer

make good way to call function from update 1 Answer

Score Keeping in unity 1 Answer

Getting score using PlayerPrefs.SetInt 1 Answer

OnControllerColliderHit counting twice towards score 0 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