Forum rules - please read before posting.

Child of Prefab

Hello,

I know this is more of a unity question but thought maybe it could help AC users if its here.

My question is very simple but I cant seem to find an answer..

With unity 2019 and the new prefab system, all I want is to assign a child attached to the player prefab from the project window folder to the "GameCamera" as the "target object". However the prefab itself from the project window is not showing any children just the main prefab, so how can I get to the child in order to assign it from the "project window"?

Comments

  • Even if it was visible, the GameCamera Inspector's Target object field only accepts objects that are in the current scene. You would have to drag the Player prefab into the scene, and assign the local scene instance of the child object - not the original prefab.

    Of course, you can always assign it through script instead:

    using UnityEngine;
    using AC;
    
    public class AssignPlayerChildToCamera : MonoBehaviour
    {
    
        private void Start ()   
        {
            GetComponent <GameCamera>().target = KickStarter.player.transform.Find ("MyChildObject");
        }
    
    }
    
  • I tried the script and it does work, but only to the first tier "children" but if I want to select for example the neck bone which is deeper inside the player it will not work. :(

  • My mistake - transform.Find only finds immediate children.

    Try GameObject.Find instead.

  • I tried GameObject.Find and I got this error

    'Player' does not contain a definition for 'GameObject' and no accessible extension method 'GameObject' accepting a first argument of type 'Player' could be found (are you missing a using directive or an assembly reference?)

  • If you get a script error, please post the actual script.

    "gameObject" must be lower-case 'g' when accessing it from a component type.

  • I tried that as well but it gave me another error.

    Member 'GameObject.Find(string)' cannot be accessed with an instance reference; qualify it with a type name instead

    using UnityEngine;
    using AC;
    
    public class Assign : MonoBehaviour
    {
    
        private void Start ()   
        {
            GetComponent <GameCamera>().target = KickStarter.player.gameObject.Find ("MyChildObject");
        }
    
    }
    
  • edited August 2019

    Try this instead:

    private void Start ()   
    {
        Transform[] children = KickStarter.player.transform.GetComponentsInChildren<Transform> ();
        foreach (var child in children)
        {
            if (child.name == "MyChildObject")
            {
                GetComponent <GameCamera>().target = child;
            }
        }
    }
    
  • I have attached it to the camera and played the scene, but nothing really happens and the 'target' slot of the GamerCamera is empty.

  • I tried to put an empty game object inside the player and named it "MyChildObject" but it didn't detect it, I also attached the "child" to the "neck bone" inside the player but same result, nothing happens..

  • My bad - the script was searching the camer's hierarchy, not the players.

    Updated the above - make sure it's attached to the GameCamera and give it another go.

  • You are a legend Chris, it worked perfectly, thanks!!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.