Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Seventh Stealth · Jul 28, 2015 at 12:39 PM · javascriptcollisiononcollisionenterstealth

OnCollisionEnter not working.

 var myZone : int;
 
 function FixedUpdate(){
 myZone = this.gameObject.transform.parent.gameObject.GetComponent(EnemyHealth).zone;
 }
 
 function OnCollisionEnter(other : Collision){
 
 if(other.gameObject.tag == "Player"){
 if(other.GetComponent(Player).zone != 227){
 if(other.GetComponent(Player).zone == myZone){
 GameObject.Find("SplashScreen").SendMessage("GameOver");
 }
 }
 }
 if(other.gameObject.tag == "Enemy"){
 Debug.Log("ENEMY : FRIENDLY");
    enemyScript = other.gameObject.GetComponent(EnemyHealth);
 if(enemyScript.zone == myZone){
 if(enemyScript.health <= 0){
 GameObject.Find("SplashScreen").SendMessage("GameOver2");
 }
 }
 }
 
 }
 


Hi. Got this script thats attached to an enemies vision box. If the vision box collides an object tagged player or an object tagged enemy with less than or equal to zero health and in the same zone as its meant to send a message to end the game. It works when it collides with the player. But when it collides with an enemy nothing happens. "ENEMY : FRIENDLY" doesnt even appear in the console so I'm guessing it doesnt register.

The enemy object has a box collider and rigidbody with isKinematic set to true. The visionbox has just a box collider attached. Theyre also tagged as enemy in the editor. The player object has a mesh collider with isTrigger set to true and a rigidbody with isKinematic set to false. It sounds like the player oject shouldnt collide but it does anyway.

So how can I get the second part of my script the part starting other.gameObject.tag =="enemy") to work.Thanks Stealth.

Comment
Add comment · Show 2
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 NeverHopeless · Jul 28, 2015 at 01:26 PM 0
Share

I$$anonymous$$HO, ToLower or ToUpper in string comparisions makes life easier:

other.gameObject.tag.ToLower() == "enemy"

avatar image tanoshimi · Jul 28, 2015 at 05:25 PM 0
Share

Add the following very simple debug:

 Debug.Log(other.gameObject.tag);

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by AlexGan001 · Jul 28, 2015 at 01:11 PM

Make sure that the enemy tag is the same as the one in the script. Capitals DO matter. if the one in the editor is "enemy" and not "Enemy" that is the problem

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 Seventh Stealth · Jul 28, 2015 at 03:51 PM 0
Share

Both have capital E's

avatar image
1

Answer by NeverHopeless · Jul 28, 2015 at 05:17 PM

Make sure enemy object is set to a layer whose collision is allowed in collision matrix under Physics setting.

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.

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

Checking to see if two objects are colliding. 2 Answers

Cannot remove a prefab using OnCollisionEnter function. 1 Answer

How can I make my truck drive on terrain and on bridge 1 Answer

OnCollisionEnter Collision not detected? 1 Answer

OnCollisionEnter Problem 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