Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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

Discussion in 'Prefabs' started by mikeohc, Aug 1, 2020.

  1. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    4
    Greetings,

    I'm wondering if there is a better way to achieve the following:
    Screenshot (178)_LI.jpg 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?
     
  2. polemical

    polemical

    Joined:
    Jun 17, 2019
    Posts:
    508
    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).
     
    mikeohc likes this.
  3. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    4
    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?

    Code (CSharp):
    1.    
    2.     public GameObject thePlayer;
    3.     [SerializeField]Transform moveTarg;
    4.  
    5.     void Start()
    6.     {
    7.         moveTarg = thePlayer.transform.GetChild(0).transform;
    8.         newRotate = transform.rotation;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.      
    14.         transform.position = moveTarg.position;
    15.  
    16.         CamControl();
    17.         mouseInput();
    18.  
    19.     }
     
  4. polemical

    polemical

    Joined:
    Jun 17, 2019
    Posts:
    508
    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.
     
  5. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    4
    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.

    Screenshot (184).png Screenshot (185).png 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.

    Code (CSharp):
    1.     //public GameObject thePlayer;
    2.     [SerializeField]Transform moveTarg;
    3.     void Start()
    4.     {
    5.         //moveTarg = thePlayer.transform.GetChild(0).transform;
    6.         newRotate = transform.rotation;
    7.     }
    8.     void Update()
    9.     {
    10.    
    11.         transform.position = moveTarg.position;
    12.         CamControl();
    13.         mouseInput();
    14.     }
     
unityunity