Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 hits 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 hit 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 something.

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

which 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 think they're colliding when one of them does. After digging through answers to see if I could access the children's script from the parent, I haven't found anyone that seems to have the same problem.

I've also tried things 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 child's collision, or have a child'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 GetComponentsInChildren that will enable you to get all children 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 child you need to attach another script to child and this will check if child is collided with wall if it is then this will call his parent to change direction.

  1. Child Script:

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

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

Might this help. if you need actual script for this 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 think 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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Make a simple tree 1 Answer

Instantiating an object on an empty gameobject?? 0 Answers

How to properly edit prefabs with child elements? 1 Answer

Access children using Find() returns 'null'? 3 Answers

How can I prevent position of a child transform to be hooked to the parent in a prefab? 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