Setting a parent as a variable in the child

Title pretty much says it all. I know this is probably really easy but I can’t figure it out. I basically have a target spawn and then after it spawns an arrow spawns and then that arrow is set as a child of the target. And then i’m trying to set the target which is now the parent as the arrow’s target.

public float Speed;
    public float Life;
    public GameObject HitPoint;


    void FixedUpdate ()
    {
        HitPoint = gameObject.GetComponentInParent<GameObject> ();
        //gameObject.transform.parent = HitPoint;
        Life -= Time.deltaTime;
        transform.LookAt (HitPoint.transform);
        transform.position += transform.forward * Speed * Time.deltaTime;

        if (Life <= 0)
        {
            Destroy(gameObject);
        }
           
    }

I know I have to be doing something wrong with the
HitPoint = gameObject.GetComponentInParent (); but I can’t figure it out. Anyone have any suggestions?

Your setup is a bit confusing, are you trying to get the GameObject of the Arrow’s parent in line 8? If so, you shouldn’t be trying to grab it as a component but rather use the direct reference through the parent itself:

HitPoint = transform.parent.gameObject; //Assuming the object this script is attached to is already parented to the target as you say.

Let me know if that helps some.

1 Like

HA! Perfect! Thanks for the help I know that was easy but I kept on getting messed up. Your beyond awesome your beysome!

1 Like

Everyone gets tripped up on the simple stuff from time to time. Don’t worry about that :). Glad I could be of some help!

1 Like