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;
}
}
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.
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.
Both objects, the player and ship must have a trigger collider attached to them.
Hello, @Christopher_$$anonymous$$tn. Are you creating a 2D game? Check the edit section of my answer.
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!
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!
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;
}
}
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.