- Home /
Make object A parent of object b
I have a player with rigid body and a sphere am using OnCollisionEnter and want to make (player/objectA parent of sphere/objectB) , if i use the script (where tag "GameController is for player and tag"sphere" is for sphere) and attached this script to sphere the sphere becomes parent of player but when i attach this script to player, player would not becomes the parrent of sphere. Any one know how to solve this problem?
//script when attached to sphere
void OnCollisionEnter(Collision col) {
Debug.Log ("collision occur");
if(col.gameObject.tag=="player")
{
col.transform.parent=transform;
}
}
//script when attached to player
void OnCollisionEnter(Collision col) {
Debug.Log ("collision occur");
if(col.gameObject.tag=="sphere")
{
col.transform.parent=transform;
}
}
Answer by static_cast · Dec 07, 2014 at 11:09 PM
You need to switch it for the player. For the player, use this: transform.parent = col.transform;
You should also look at http://docs.unity3d.com/ScriptReference/Transform.SetParent.html, as transform.parent is deprecated.
Your answer
Follow this Question
Related Questions
How to destroy a parent object when child object is collided 1 Answer
Make a simple tree 1 Answer
Player object still gets destroyed even when shields up 1 Answer
How do i kill an "enemy with character controller on + few child objects on" 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers