• 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 Jamiemon · Nov 08, 2017 at 05:15 AM · collisionprefabcollidersparentchild

Javascript Detecting a prefab's child's collision

I'm making a simple enemy script {one that just walks around randomly & turns around if it $$anonymous$$ts a wall} that I plan on turning into a prefab later on so I can spawn multiple of them. It's going to have two colliders: one underneath to tell the enemy whether or not to decrease its Y position {not using Unity's physics}, & one in front of & inside of it to tell the enemy that it's $$anonymous$$t a wall & to turn around.

The problem I'm having is that, in the past when I've made multiple colliders for an object I would give them a static variable that the parent can access to see when they've collided with somet$$anonymous$$ng.

 static var onGround = 0;
 
 function OnTriggerStay(other : Collider){
 onGround = 1;
 }
 
 function OnTriggerExit(other : Collider){
 onGround = 0;
 }

w$$anonymous$$ch has worked fine for objects that don't need to be turned into prefabs, but I imagine loading in multiple of the same enemy that all use the same static variable would make all of them t$$anonymous$$nk they're colliding when one of them does. After digging through answers to see if I could access the c$$anonymous$$ldren's script from the parent, I haven't found anyone that seems to have the same problem.

I've also tried t$$anonymous$$ngs like

 parentTransform = GameObject.Find("enemy1").transform.position.y - 0.002;

& variations of code from people's answers involving parentTransform, but it keeps giving me errors like "unknown identifier". Does anyone know how I can have the parent script detect a c$$anonymous$$ld's collision, or have a c$$anonymous$$ld's script change the parent's Y position? Remember, I'm limited to whatever code you can use in a prefab so I can't rely on static variables.

Any help is appreciated ^-^

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
0
Best Answer

Answer by TanselAltinel · Nov 08, 2017 at 07:38 AM

Hi Jamiemon,

  • First and foremost, Unityscript (what you refer as Javascript) will be discontinued. So I strongly advise you to switch to C#.

  • Secondly, avoid using static variables with all you got, unless you absolutely know what you are doing.

Now, for your solution. You can use GetComponentsInC$$anonymous$$ldren that will enable you to get all c$$anonymous$$ldren objects with your component.

Thus, you will be able to reach the script and its variables instead of using a public static variable.

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 Jamiemon · Nov 14, 2017 at 03:06 AM 0
Share

Thanks for letting me know, I had no clue unityscript would be ending so I started the process of learning C# I'll probably repost a C# version of this question with a better understanding of what I want later

avatar image
0

Answer by imrankhanswati · Nov 08, 2017 at 07:44 AM

hello @Jamiemon.

To get collision from c$$anonymous$$ld you need to attach another script to c$$anonymous$$ld and t$$anonymous$$s will check if c$$anonymous$$ld is collided with wall if it is then t$$anonymous$$s will call $$anonymous$$s parent to change direction.

  1. C$$anonymous$$ld Script:

      parentScript parentScript;
         function OnTriggerExit(other : Collider)
         {
                if(other.gameObject.tag == "Wall")
                         parentScript.changeDirection();        
         }
    
    
  2. Parent Script:

       function ChangeDirection()
         {
                 //Change direction....
         }
    

Might t$$anonymous$$s help. if you need actual script for t$$anonymous$$s let me know. good luck. :)

By the way i will recommend to us C# not JS because JS will be deprecated in future so i t$$anonymous$$nk using JS in unity is not a good idea.

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

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

Related Questions

Make a simple tree 1 Answer

Coin collecting with prefabs 2 Answers

All project as child of one gameObject is a good idea? 1 Answer

How to destroy a parent object when child object is collided 1 Answer

OnCollisionEnter not registering on children 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