• 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 srafciger · Jun 19, 2011 at 05:38 PM · score

Collision Counting Problem

I have a problem with my script, and need someone to help me with it.

Script written below is attached to game object called Ball and what I want is that script counts number of collision that happen in specific order (Box, Cube, Floor...First hits Box, then Cube and finally Floor).

My script doesn't do a thing. pointsPlayer1 is 0 all the time.

Anyone who can help me?

 static var pointsPlayer1 = 0;
 var MetalGUISkin : GUISkin;
 
 
 function OnCollisionEnter(hit : Collision) {
     if (hit.gameObject.name == "Box" ) {
         if (hit.gameObject.name == "Cube"){
             if (hit.gameObject.name == "Floor"){
             pointsPlayer1++;
             }
         }        
     }
 }
 
 function  OnGUI(){
     
 GUI.skin = MetalGUISkin;
 GUI.Label(Rect(25,200,35,25),pointsPlayer1.ToString());
 }
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
2
Best Answer

Answer by Joshua · Jun 19, 2011 at 05:48 PM

What your script currently does is test to see if the collision object is named Box and Cube and Floor, which will never be true of course.

Try something like this:

 var hitBox : boolean = false;
 var hitCube : boolean = false;
 function OnCollisionEnter( hit : Collision ){
     if( !hitBox && hit.gameObject.name == "Box" ){
         hitBox = true;
         hitCube = false;
     }
     else if( hitBox && !hitCube && hit.gameObject.name == "Cube" ){
         hitBox = false;
         hitCube = true;
     }
     else if( hitCube && hit.gameObject.name ==  "Floor" ){
         pointsPlayer1++;
         hitCube = false;
     }
 }

If you're going to have more complicated 'combos' you could also add the name of the object to an array and then check the array for the combo.

Comment
Add comment · 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
1

Answer by GuyTidhar · Jun 19, 2011 at 06:00 PM

You will never reach the second "if" statement. OnCollisionEnter() is called each time an object enters the collider found on the same GameObject as the script you are calling OnCollisionEnter(). You are checking if the name of the object that hit yours, is "Box", and only if it, do you check if that exact game Object is named "Cube". This is not logical and will never happen. You need to keep track of what was hit, and each time a new hit is listed, you need to check the track and keep at it or reset it if the order is wrong. For instance: The simple to understand solution (there is a much nicer one, but it might be too complicated for you to understand at this point):

 var hitList : int = 0;
 function OnCollisionEnter(hit : Collision) 
 {
     if (hit.gameObject.name == "Box" )
     {
        hitList = 1;
     }
     else if (hit.gameObject.name == "Cube")
     {
        if ( hitList == 1 ) 
          hitList = 2;
        else
          hitList = 0;
     }
     else if (hit.gameObject.name == "Floor")
     {
        if ( hitList == 2 )
           pointsPlayer1++;

        hitList = 0;
     }
 }

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity Game Score Script 1 Answer

INCREASE SCORE MORE AND MORE? 1 Answer

How to go about purchasing objects with player score/points? 1 Answer

Multiplayer ScoreBoard not Updating Correctly 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