Cannot get the child of a GameObject through script [duplicate]












0















This question already has an answer here:




  • How to find child of a GameObject or the script attached to child GameObject via script

    3 answers




I want to access the animator component of my player character. The character is spawned under the GameObject Character position, which it self is the child of Game Manager.



The character prefabs have various names, so I cannot find them through exact name. So its easier to just get the only child of Character position.



Game Manager
Character position
Player Prefab


Ive searched online and tried GetChild by index and GetComponentInChildren. None of them work. Below is the script I wrote for this:



    private Animator archerAnimator;
private float startSpeed;

GameObject charPos;
GameObject archer_;
// Use this for initialization
void Start () {
charPos = GameObject.Find("Game manager/Character position");
Debug.Log(charPos);
archer_ = charPos.transform.GetChild(0).gameObject;
archerAnimator = charPos.GetComponentInChildren<Animator>();
Debug.Log(archerAnimator);
}


charPos is found, but for archer_ I get the error, Transform child out of bounds. The player archer is not there but is spawned at run time when the scene starts, is this the reason it cannot find it so quickly?



Some guidance would be appreciated.



Thank you










share|improve this question













marked as duplicate by Programmer unity3d
Users with the  unity3d badge can single-handedly close unity3d questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 15:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • So the problem is your trying to find a game object that does not exist yet?
    – CaTs
    Nov 12 at 22:55










  • I'm not really sure. Thats what I think may be happening. As the player is spawned as soon as the game starts. Maybe I'm trying to find it before its spawned. How to resolve this?
    – StuckInPhD
    Nov 12 at 22:58






  • 1




    Show how ans where you're spawning the character via code. Also show the Hierarchy tab screenshot that shows the character when you spawn it
    – Programmer
    Nov 13 at 0:05










  • When you have some actions related to each others but are not in the same script, avoid declaring your logic in the start function, because if you are Instantiating the object and its children in another script within the start method, you can't guarantee or know which start will be triggered first. Instead, you can declare starting logic in an Init() function and then call them in order in the manager. (Another way to fix this without using the solution above is to change the execution order rules in Unity) Happy coding!
    – GhAyoub
    Nov 13 at 5:25


















0















This question already has an answer here:




  • How to find child of a GameObject or the script attached to child GameObject via script

    3 answers




I want to access the animator component of my player character. The character is spawned under the GameObject Character position, which it self is the child of Game Manager.



The character prefabs have various names, so I cannot find them through exact name. So its easier to just get the only child of Character position.



Game Manager
Character position
Player Prefab


Ive searched online and tried GetChild by index and GetComponentInChildren. None of them work. Below is the script I wrote for this:



    private Animator archerAnimator;
private float startSpeed;

GameObject charPos;
GameObject archer_;
// Use this for initialization
void Start () {
charPos = GameObject.Find("Game manager/Character position");
Debug.Log(charPos);
archer_ = charPos.transform.GetChild(0).gameObject;
archerAnimator = charPos.GetComponentInChildren<Animator>();
Debug.Log(archerAnimator);
}


charPos is found, but for archer_ I get the error, Transform child out of bounds. The player archer is not there but is spawned at run time when the scene starts, is this the reason it cannot find it so quickly?



Some guidance would be appreciated.



Thank you










share|improve this question













marked as duplicate by Programmer unity3d
Users with the  unity3d badge can single-handedly close unity3d questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 15:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • So the problem is your trying to find a game object that does not exist yet?
    – CaTs
    Nov 12 at 22:55










  • I'm not really sure. Thats what I think may be happening. As the player is spawned as soon as the game starts. Maybe I'm trying to find it before its spawned. How to resolve this?
    – StuckInPhD
    Nov 12 at 22:58






  • 1




    Show how ans where you're spawning the character via code. Also show the Hierarchy tab screenshot that shows the character when you spawn it
    – Programmer
    Nov 13 at 0:05










  • When you have some actions related to each others but are not in the same script, avoid declaring your logic in the start function, because if you are Instantiating the object and its children in another script within the start method, you can't guarantee or know which start will be triggered first. Instead, you can declare starting logic in an Init() function and then call them in order in the manager. (Another way to fix this without using the solution above is to change the execution order rules in Unity) Happy coding!
    – GhAyoub
    Nov 13 at 5:25
















0












0








0








This question already has an answer here:




  • How to find child of a GameObject or the script attached to child GameObject via script

    3 answers




I want to access the animator component of my player character. The character is spawned under the GameObject Character position, which it self is the child of Game Manager.



The character prefabs have various names, so I cannot find them through exact name. So its easier to just get the only child of Character position.



Game Manager
Character position
Player Prefab


Ive searched online and tried GetChild by index and GetComponentInChildren. None of them work. Below is the script I wrote for this:



    private Animator archerAnimator;
private float startSpeed;

GameObject charPos;
GameObject archer_;
// Use this for initialization
void Start () {
charPos = GameObject.Find("Game manager/Character position");
Debug.Log(charPos);
archer_ = charPos.transform.GetChild(0).gameObject;
archerAnimator = charPos.GetComponentInChildren<Animator>();
Debug.Log(archerAnimator);
}


charPos is found, but for archer_ I get the error, Transform child out of bounds. The player archer is not there but is spawned at run time when the scene starts, is this the reason it cannot find it so quickly?



Some guidance would be appreciated.



Thank you










share|improve this question














This question already has an answer here:




  • How to find child of a GameObject or the script attached to child GameObject via script

    3 answers




I want to access the animator component of my player character. The character is spawned under the GameObject Character position, which it self is the child of Game Manager.



The character prefabs have various names, so I cannot find them through exact name. So its easier to just get the only child of Character position.



Game Manager
Character position
Player Prefab


Ive searched online and tried GetChild by index and GetComponentInChildren. None of them work. Below is the script I wrote for this:



    private Animator archerAnimator;
private float startSpeed;

GameObject charPos;
GameObject archer_;
// Use this for initialization
void Start () {
charPos = GameObject.Find("Game manager/Character position");
Debug.Log(charPos);
archer_ = charPos.transform.GetChild(0).gameObject;
archerAnimator = charPos.GetComponentInChildren<Animator>();
Debug.Log(archerAnimator);
}


charPos is found, but for archer_ I get the error, Transform child out of bounds. The player archer is not there but is spawned at run time when the scene starts, is this the reason it cannot find it so quickly?



Some guidance would be appreciated.



Thank you





This question already has an answer here:




  • How to find child of a GameObject or the script attached to child GameObject via script

    3 answers








unity3d






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 22:03









StuckInPhD

79432041




79432041




marked as duplicate by Programmer unity3d
Users with the  unity3d badge can single-handedly close unity3d questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 15:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Programmer unity3d
Users with the  unity3d badge can single-handedly close unity3d questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 15:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • So the problem is your trying to find a game object that does not exist yet?
    – CaTs
    Nov 12 at 22:55










  • I'm not really sure. Thats what I think may be happening. As the player is spawned as soon as the game starts. Maybe I'm trying to find it before its spawned. How to resolve this?
    – StuckInPhD
    Nov 12 at 22:58






  • 1




    Show how ans where you're spawning the character via code. Also show the Hierarchy tab screenshot that shows the character when you spawn it
    – Programmer
    Nov 13 at 0:05










  • When you have some actions related to each others but are not in the same script, avoid declaring your logic in the start function, because if you are Instantiating the object and its children in another script within the start method, you can't guarantee or know which start will be triggered first. Instead, you can declare starting logic in an Init() function and then call them in order in the manager. (Another way to fix this without using the solution above is to change the execution order rules in Unity) Happy coding!
    – GhAyoub
    Nov 13 at 5:25




















  • So the problem is your trying to find a game object that does not exist yet?
    – CaTs
    Nov 12 at 22:55










  • I'm not really sure. Thats what I think may be happening. As the player is spawned as soon as the game starts. Maybe I'm trying to find it before its spawned. How to resolve this?
    – StuckInPhD
    Nov 12 at 22:58






  • 1




    Show how ans where you're spawning the character via code. Also show the Hierarchy tab screenshot that shows the character when you spawn it
    – Programmer
    Nov 13 at 0:05










  • When you have some actions related to each others but are not in the same script, avoid declaring your logic in the start function, because if you are Instantiating the object and its children in another script within the start method, you can't guarantee or know which start will be triggered first. Instead, you can declare starting logic in an Init() function and then call them in order in the manager. (Another way to fix this without using the solution above is to change the execution order rules in Unity) Happy coding!
    – GhAyoub
    Nov 13 at 5:25


















So the problem is your trying to find a game object that does not exist yet?
– CaTs
Nov 12 at 22:55




So the problem is your trying to find a game object that does not exist yet?
– CaTs
Nov 12 at 22:55












I'm not really sure. Thats what I think may be happening. As the player is spawned as soon as the game starts. Maybe I'm trying to find it before its spawned. How to resolve this?
– StuckInPhD
Nov 12 at 22:58




I'm not really sure. Thats what I think may be happening. As the player is spawned as soon as the game starts. Maybe I'm trying to find it before its spawned. How to resolve this?
– StuckInPhD
Nov 12 at 22:58




1




1




Show how ans where you're spawning the character via code. Also show the Hierarchy tab screenshot that shows the character when you spawn it
– Programmer
Nov 13 at 0:05




Show how ans where you're spawning the character via code. Also show the Hierarchy tab screenshot that shows the character when you spawn it
– Programmer
Nov 13 at 0:05












When you have some actions related to each others but are not in the same script, avoid declaring your logic in the start function, because if you are Instantiating the object and its children in another script within the start method, you can't guarantee or know which start will be triggered first. Instead, you can declare starting logic in an Init() function and then call them in order in the manager. (Another way to fix this without using the solution above is to change the execution order rules in Unity) Happy coding!
– GhAyoub
Nov 13 at 5:25






When you have some actions related to each others but are not in the same script, avoid declaring your logic in the start function, because if you are Instantiating the object and its children in another script within the start method, you can't guarantee or know which start will be triggered first. Instead, you can declare starting logic in an Init() function and then call them in order in the manager. (Another way to fix this without using the solution above is to change the execution order rules in Unity) Happy coding!
– GhAyoub
Nov 13 at 5:25














1 Answer
1






active

oldest

votes


















1














I think you're scanning for the player too early. You should reverse your discovery logic. Instead of scanning for the player and its Animator, you should put a script on the player itself that runs after it is created and reports itself to the game manager or whatever object needs access to it, something like this:



void Start() { GetComponentInParent<GameManager>().OnPlayerSpawned(this); }


I'll also mention that some script finding an object by name and accessing its components is a generally bad idea. Here's a design guideline to always keep in mind: You should traverse Unity's object hierarchy as infrequently as possible, and even if you do, you should only traverse objects that don't have other scripts attached. In this case, you should also put the logic to control the Animator inside your Player script. Then, you wouldn't need to get a reference to the Animator in the first place.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I think you're scanning for the player too early. You should reverse your discovery logic. Instead of scanning for the player and its Animator, you should put a script on the player itself that runs after it is created and reports itself to the game manager or whatever object needs access to it, something like this:



    void Start() { GetComponentInParent<GameManager>().OnPlayerSpawned(this); }


    I'll also mention that some script finding an object by name and accessing its components is a generally bad idea. Here's a design guideline to always keep in mind: You should traverse Unity's object hierarchy as infrequently as possible, and even if you do, you should only traverse objects that don't have other scripts attached. In this case, you should also put the logic to control the Animator inside your Player script. Then, you wouldn't need to get a reference to the Animator in the first place.






    share|improve this answer


























      1














      I think you're scanning for the player too early. You should reverse your discovery logic. Instead of scanning for the player and its Animator, you should put a script on the player itself that runs after it is created and reports itself to the game manager or whatever object needs access to it, something like this:



      void Start() { GetComponentInParent<GameManager>().OnPlayerSpawned(this); }


      I'll also mention that some script finding an object by name and accessing its components is a generally bad idea. Here's a design guideline to always keep in mind: You should traverse Unity's object hierarchy as infrequently as possible, and even if you do, you should only traverse objects that don't have other scripts attached. In this case, you should also put the logic to control the Animator inside your Player script. Then, you wouldn't need to get a reference to the Animator in the first place.






      share|improve this answer
























        1












        1








        1






        I think you're scanning for the player too early. You should reverse your discovery logic. Instead of scanning for the player and its Animator, you should put a script on the player itself that runs after it is created and reports itself to the game manager or whatever object needs access to it, something like this:



        void Start() { GetComponentInParent<GameManager>().OnPlayerSpawned(this); }


        I'll also mention that some script finding an object by name and accessing its components is a generally bad idea. Here's a design guideline to always keep in mind: You should traverse Unity's object hierarchy as infrequently as possible, and even if you do, you should only traverse objects that don't have other scripts attached. In this case, you should also put the logic to control the Animator inside your Player script. Then, you wouldn't need to get a reference to the Animator in the first place.






        share|improve this answer












        I think you're scanning for the player too early. You should reverse your discovery logic. Instead of scanning for the player and its Animator, you should put a script on the player itself that runs after it is created and reports itself to the game manager or whatever object needs access to it, something like this:



        void Start() { GetComponentInParent<GameManager>().OnPlayerSpawned(this); }


        I'll also mention that some script finding an object by name and accessing its components is a generally bad idea. Here's a design guideline to always keep in mind: You should traverse Unity's object hierarchy as infrequently as possible, and even if you do, you should only traverse objects that don't have other scripts attached. In this case, you should also put the logic to control the Animator inside your Player script. Then, you wouldn't need to get a reference to the Animator in the first place.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 3:42









        Arshia001

        700510




        700510















            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?