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 pjde_ · Apr 10, 2014 at 11:27 AM · collisiontrigger

Detect Collision in an If Statement?

Basically, what I'm doing is this.

If the player presses a button: If a certain object (already made a variable) is colliding with another object (also a variable) add 1 to points. Else start game over phase.

Any ideas how this can be implemented? Specifically the colliding part as everything else I already have implemented...

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

Answer by maddFrogg · Apr 10, 2014 at 11:34 AM

Try this:

 void OnCollisionStay(Collision collisionInfo) {
     if ( Input.GetMouseButtonDown(0) {
         if (collisionInfo.gameObject.name == nameofYourVariable) {
              // Do stuff
         }
     }
 }

This method should be in a class associated to the object NOT called nameofYourVariable

Note: Both objects must have a Collider and a Rigidbody associated.

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 pjde_ · Apr 11, 2014 at 08:14 AM 0
Share

Thanks for the response but will this work if none of the two objects has this script attatched to it?

avatar image Salzmann · Apr 11, 2014 at 08:48 AM 0
Share

The OnCollisionStay $$anonymous$$ethod will only be called if it's in a script that exists on the colliding object. However, you can just do stuff like this:

 //this class is on the colliding gameobject
 void OnCollisionStay(Collision collisionInfo) 
 {
 Collision$$anonymous$$anager.Singleton.HandleCollision(gameObject, collisionInfo);
 }

And the other class would look something like this, and can be anywhere in the scene.

 public static Collision$$anonymous$$anager Singleton;
 
 void Start
 {
 Singleton = this;
 }
 
 public void HandleCollision(GameObject source, Collision collisionInfo)
 {
 //do stuff with the info
 }

Important note: In this case, the Collision$$anonymous$$anager should exist only once in your entire scene at all times.

avatar image pjde_ · Apr 13, 2014 at 04:48 AM 0
Share

Thank you for all the replies guys! I decided to go with the original script and move the Game script which controls everything to the object since it wouldn't make a difference to how the game plays haha.

The thing is, nothing happens. I did everything correctly and what not but when I click nothing happens. I made an else statement to restart the level if they weren't colliding to test and nothing still happens. Any ideas?

     void OnCollisionStay2D (Collision2D collisionInfo){
         if(Input.Get$$anonymous$$ouseButtonDown(0)){
             if(collisionInfo.gameObject == line){
                 score = score + 1;
                 print ("it worked!");
             }
             else{
                 Application.LoadLevel("level");
             }
         }
 
     }

thanks for the help!

avatar image Salzmann · Apr 14, 2014 at 08:15 AM 0
Share

Please check if both the gameobject with this script and the gameobject being hit both have a Collider2D attached and one of them has a rigidbody2D. They must not be triggers, and the rigidbody must not be kinematic. If you dont want any collision but still want them to react to each other, turn both colliders into triggers and use OnTriggerStay2D() ins$$anonymous$$d.

avatar image pjde_ · Apr 14, 2014 at 10:55 AM 0
Share

arrghh the issue with that is that they have to be kinematic :/ ill try using ontriggerstay ins$$anonymous$$d. thanks for the tip! :D

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Trying to animate on collision, Help Please. 0 Answers

How to call OnTriggerEnter once 5 Answers

how to go through platform like bananakong? 1 Answer

Scene Change Collision 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