• 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 Xeong-Hu · Feb 25, 2014 at 10:32 AM · javascriptcollision

How to cause an effect when an Object Collides with another Object.

Alright look. I've searched up and down with this thing in trying to make an effect happen when ever my object collides with another.

Here's what i'm trying to do. I'm trying to make an area occupied when ever i place an object down in that specific spot.

For example. If a Card is placed in Zone 1. Zone 1 is then occupied and no other card can go there.

Here's my Code.

 function OnCollisionEnter( hit : Collision){
     if (hit.gameObject.name == collisionObject2.name){
        Occupied = true;
                      if (Occupied == true){
                    Debug.Log("This Spot is Occupied");
                    } 
        
     }
 }

collisionObject2 is supposed to be my Spot 1 Object.

I've been doing this thing and it never Debugs "This Spot is Occupied" nor does it even make my var Occupied true. I watched it.

Please can anyone help me from this madness? I'm losing it..

Comment
Add comment · Show 9
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 CHPedersen · Feb 25, 2014 at 10:35 AM 0
Share

The problem here is that the first if-statement never evaluates to true. I.e. hit.gameObject.name is never equal to collisionObject2.name.

Put a Debug.Log outside the top if-statement, and print those names to see what they are.

avatar image brent ragsdale · Feb 25, 2014 at 10:38 AM 0
Share

Are you making a 2D card game? Grid based systems are easy to implement using hashtables.

avatar image Xeong-Hu · Feb 26, 2014 at 07:55 AM 0
Share

@CHPedersen

I dont know if I understood what you said about my 1st if statement doesn't evaluate to true. But i tried changing it to the best of my ability on your words. here's my new codes.

 function OnCollisionEnter( hit : Collision){
                    Debug.Log(collisionObject2.name);
     hit.GameObject.name == collisionObject2.name;{
             if(hit.GameObject.name == collisionObject2.name == true){
        Occupied = true;{
                      if (Occupied == true){
                     Debug.Log(collisionObject2.name);
                    } 
        }
     }
    }
 }



I made a var named hit and gave it a game object. Within that Gameobject I put the object named Card.

I put the Debug on top like you said and yet the Computer still doesnt print out the name of the object. But When i put the Debug in my Function Start it worked just fine. So. I think my Function OnCollisionEnter is really F'd up but idk wat the problem is. Am i using the wrong function?

I really need help fixing this thing.

avatar image Jamora · Feb 26, 2014 at 08:30 AM 0
Share

You should be able to see why your if-statement is not entered by having a Debug.Log like this:

 function OnCollisionEnter( hit : Collision){
 
     Debug.Log("Is "+ hit.gameObject.name +" the same as "+collisionObject2.name+" ?");
 
     if (hit.gameObject.name == collisionObject2.name){
         Debug.Log("Result: Yes!");
         Occupied = true;
         if (Occupied == true){
             Debug.Log("This Spot is Occupied");
         }
     }else{ Debug.Log("Result: No ...");}
 }
avatar image Xeong-Hu · Feb 26, 2014 at 08:58 AM 0
Share

Thanks for replying. i just added that code. But i get absolutely nothing showing up on the console. Its like my Collision Function doesn't even exist.

The only thing it gives me is a warning sign saying. "The referenced script on this behaviour is missing"

That's all it shows.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RedDevil · Feb 26, 2014 at 09:39 AM

You might get better results by using tags: ex:

function OnTriggerEnter(col : Collider) { if(col.tag =="thetagyouchose")
{

//code you want to execute
} }
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 Xeong-Hu · Feb 26, 2014 at 09:57 AM 0
Share

Thanks for the support. But damn that didn't work either. I think my problem with reading this thing is also screwing me over.

Can some one please explain to me this. function OnTriggerEnter(col : Collider)

Like I kn ow it's a collision function. But what does the col mean? and then this little thingy right here " : " And i'm just going to guess Collision means the effect it'll have on the col object.

Wat i'm confused about is. What is this col? i mean how does the unity script know what it is? Like is it an Object? my Object? It's confusing me.

avatar image Jamora · Feb 26, 2014 at 10:11 AM 0
Share

OnTriggerEnter is used when a trigger enters a collider or another trigger. Do your box colliders have the trigger checkmark checked?

avatar image RedDevil · Feb 26, 2014 at 10:19 AM 0
Share

Well that is just basic javascripting. col : Collider is just like saying in C# Collider col and it means you are creating an Collider type object named col. Jamora asked a good questions.Did you check to see if your Collider has the " Is Trigger" Checked?If not this function will not be called at all. I see you have some Debug.Log 's in your code! Do you see any of those in the console?

avatar image Xeong-Hu · Feb 26, 2014 at 10:20 AM 0
Share

Uh huh. yep.

avatar image RedDevil · Feb 26, 2014 at 10:22 AM 0
Share

Did you put a tag on the object you want to check the tag for?

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.

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

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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Custom Collision Detection 4 Answers

Substitute of collision 2 Answers

Attack animation Wont Play 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