Assigning a child of existing prefab onto another prefab's target

Greetings,

I’m wondering if there is a better way to achieve the following:

6158294--673121--Screenshot (179)_LI.jpg

Basically, I want to attach a child of a prefab onto another prefab. Currently, I am doing it manually. Is there a better way to do this?

Since “Move Targ” in the Inspector accepts a Transform, you can just assign the .transform of any Game Object to it. For example, controller.moveTarg = obj.transform;
If you wanted to assign the Sphere to it with the hierarchy you have from a script on your CamRig, you could say moveTarg = transform.parent.Find(“Player”).Find(“Sphere”);
Note this is fine if you’re doing it on occasion but you don’t want to call .Find every frame (re performance).

1 Like

Thanks for the response,

This method does indeed get the Sphere Transform, but I’m running into an issue where the position of the Sphere isn’t updating for the camera to track now. It’s returning 0,0,0 when I used Debug.Log but the position is changing in the inspector. Any idea why that’s the case?

    public GameObject thePlayer;
    [SerializeField]Transform moveTarg;

    void Start()
    {
        moveTarg = thePlayer.transform.GetChild(0).transform;
        newRotate = transform.rotation;
    }

    void Update()
    {
      
        transform.position = moveTarg.position;

        CamControl();
        mouseInput();

    }

I haven’t seen enough of what you’re doing to say - but where you set moveTarg = …, GetChild already returns the transform, you don’t need to say “.transform” at the end.

Thanks

Essentially, I’m using position of Sphere to move CamRig around, which has a Camera child. You can see below that moveTarg is targetting Sphere, and Sphere’s position is updating in the inspector. Problem is that it’s returning (0,0,0) so that means CamRig will remain at zero as well.

6158525--673148--Screenshot (184).png 6158525--673151--Screenshot (185).png 6158525--673154--Screenshot (186).png

Prior to this, I’d just manually drag Sphere onto moveTarg to get the position with the code below, and that works.

    //public GameObject thePlayer;
    [SerializeField]Transform moveTarg;
    void Start()
    {
        //moveTarg = thePlayer.transform.GetChild(0).transform;
        newRotate = transform.rotation;
    }
    void Update()
    {
   
        transform.position = moveTarg.position;
        CamControl();
        mouseInput();
    }