Hi im learning unity so im curently new at this. For my game I want my Character went It touches the ball of the game it attach to hes hand.
This is my current code:
public GameObject player;
public GameObject ball;
void OnTriggerEnter (Collider other)
{
if(other.tag == "Player")
{
ball.transform.parent = player.transform;
Instantiate(ball, transform.position, transform.rotation);
}
}
}
If I understand you correctly then you have a player and a ball in the scene and when the ball touches the player it should become a child of the player, right? And I am correct that this script is on the ball object and that the “player” variable is a reference to the player object in the scene?
If yes, first make sure that your player object has the tag “Player”. Then change your OnTriggerEnter code in to this:
void OnTriggerEnter (Collider other)
{
if(other.tag == "Player")
{
if (player != null)
{
transform.parent = player.transform;
}
}
}
This code tells the object that the script is on (which I assume is the ball) to become a child of the object that has the tag “Player”.