Cannot get the child of a GameObject through script [duplicate]
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
unity3d
marked as duplicate by Programmer
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.
add a comment |
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
unity3d
marked as duplicate by Programmer
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
add a comment |
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
unity3d
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
unity3d
asked Nov 12 at 22:03
StuckInPhD
79432041
79432041
marked as duplicate by Programmer
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 at 3:42
Arshia001
700510
700510
add a comment |
add a comment |
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