Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Pixelen · Jan 05, 2012 at 07:47 PM · collisionscorecounter

Score updating after collision

Hi, I'm a total beginner to Unity and I just needed some help with scoring. At the moment what I've got is this:

 var Counter : int = 0;
 
 function Update () {
     guiText.text = "Score: "+Counter;
     Counter+=10;
 }

Which updates the score constantly, but what I want is for the score to increase by 10 when there is a collision between two objects. Is there anyway I can specify this? Thanks in advance.

Update: Yay, I figured it out! Basically I wanted the score to increase whenever the basketball hit the target. This is the code I've now got attached to the score:

 var Counter : int = 0;      
 
 function Update () {    
     guiText.text = "Score: "+Counter;
     }   
         
 function React () {            
     Counter++;    
     }

And this code attached to the basketball:

 function OnCollisionEnter(theCollision : Collision){
 
    if(theCollision.gameObject.name == "Target"){ 
       gameObject.Find("Score").SendMessage("React");       
       Debug.Log("Score!");  
       yield WaitForSeconds(1);
       gameObject.active=false;
       }
 }

Thanks for the help!

Comment
Add comment · Show 1
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 Apples_mmmmmmmm · Jan 07, 2012 at 07:28 PM 0
Share

Updated answer with correct code. Also, for reference, next time ins$$anonymous$$d of creating a new answer, simply comment on an old one. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Apples_mmmmmmmm · Jan 06, 2012 at 03:08 AM

Make a Scorecounter script and attach this to your gui.Text object.

static var Counter : int = 0;

function Update () {
guiText.text = "Score: " + Counter; }

Make a scoretrigger script and attach this to the trigger that you want the ball to enter. Make sure that the checkbox 'isTrigger' on the gameobject is checked. Make sure that the object you're using as a ball is marked with a tag called ball (or if called something else, just change the CompareTag string to the tag's name).

function OnTriggerEnter(enterer : Collider) 
{
    if (enterer.collider.gameObject.CompareTag("ball"))
    {
        Debug.Log("Score!");
        Scorecounter.Counter ++;
    }
}

Comment
Add comment · Show 1 · 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 Lboy · Apr 22, 2012 at 02:50 AM 0
Share

This is really usefull, thanks guys.

But how would I go about respawning my ball on collision?

Thanks,

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

7 People are following this question.

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

Related Questions

how to keep track of bricks broken 1 Answer

Scoring (i'm stuck) 3 Answers

Score count increase on hit 2 Answers

Collision with cube isn't working when Instantiate clone it 2 Answers

Trying to get score counter... 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