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 /
  • Help Room /
avatar image
0
Question by Christopher_Mtn · Feb 15, 2016 at 05:06 AM · c#parentchild

Parenting objects with code not working

So, I am trying to do something relatively basic that I just can't seem to figure out. I am trying to parent my player to my spaceship so that when I step on to my ship I will move around with it. However, I have never done this exact call before, so I don't know the pitfalls of it. Essentially there are no errors, but when I get on nothing happens in the heirarchy, and my player does not move with the ship.

Here is my code, any help is greatly appreciated!

 void OnTriggerEnter(Collider other)
     {
         if(other.tag == "Player")
         {
             player.transform.parent = Ship.transform;
         }
     }
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 EmHuynh · Feb 16, 2016 at 01:33 AM

Hey, @christopher_mtn! You will need to attach a rigidbody to one of the colliders (ship or player).

Reference: MonoBehaviour.OnTriggerEnter(Collider)

Description:

OnTriggerEnter is called when the Collider other enters the trigger.

This message is sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Notes: Trigger events are only sent if one of the colliders also has a rigidbody attached. Trigger events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

Edit:

If you are creating a 2D game, you should be calling OnTriggerEnter2D to check for collisions by Collider2D.

Here is the documentation for OnTriggerEnter2D( Collider2D ).

Here is the documentation for Collider2D.

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 Christopher_Mtn · Feb 18, 2016 at 04:34 AM 0
Share

Yeah, the ship already has a rigidbody on it. Do I need to assign the trigger collider somehow? Otherwise I can't think of anything else, but thank you all so far for giving it a shot.

avatar image EmHuynh Christopher_Mtn · Feb 18, 2016 at 06:45 AM 0
Share

Both objects, the player and ship must have a trigger collider attached to them.

avatar image EmHuynh Christopher_Mtn · Feb 18, 2016 at 08:33 AM 0
Share

Hello, @Christopher_$$anonymous$$tn. Are you creating a 2D game? Check the edit section of my answer.

avatar image Christopher_Mtn EmHuynh · Feb 20, 2016 at 02:00 AM 0
Share

OH! Both the objects have to have a trigger collider? I thought it was just the ship, thank you so much! Also, will the player need to have a rigidbody? I don't have one attached right now, and my parent wouldn't really work with them both having rigidbody's.

Anyways, thank you so much! I am going to go try it right now!

Show more comments
avatar image Christopher_Mtn · Feb 20, 2016 at 11:30 PM 0
Share

Yeah, that was it! Thanks so much for that, I didn't realize there would be a difference between ontriggerenter 2d and 3d.

Anyways, it works! Thanks so much!

avatar image
0

Answer by toddisarockstar · Feb 15, 2016 at 05:24 AM

code looks good. its pry something else. maybe you are missing a collider or something. try something like this to make sure your getting the collition.

      void OnTriggerEnter(Collider other)
          {
              if(other.tag == "Player")
              {  print("this should print if I got the part collition right!!!!! ");
                  player.transform.parent = Ship.transform;
              }
          }

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 Christopher_Mtn · Feb 16, 2016 at 01:06 AM 0
Share

Hmmm, the object did not seem to collide according to the print. The trigger collider is on the ship, should it be on the player? Also the script is on the ship, so should that be on the player? I can't think of any reasons why the colliders would not be working, although could both of those objects being rigidbody's have anything to do with it? I feel very lost.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why can't I access the public variables of the children class of TestItem in this syntax? I only have access to TestItem variables 1 Answer

Can not access GetComponentInParent ( ). usegravity 1 Answer

[PUN] Change Parent's GameObject in Network 0 Answers

Child an Object to Another Objects Parent On Collision 1 Answer

Instantiating as a child of an object in hierarchy 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