- Home /
Child Transform Out of Bounds
Hello all! I am trying to scale specific parts of a 2D character and have all of the child objects scale and position themselves accordingly.
The scaling works fine but when I try to reposition the child to be where it should reletively be I get the exception
UnityException: Transform child out of bounds
First I'm wondering what the exception is and second how it is thrown.
Here is my code
private void BoneScale()
{
Transform[] tempBone = knight.mBoneTransforms;
foreach (Transform gameObj in tempBone)
{
if (gameObj.parent != null)
{ //Look for specific bone
if (gameObj.parent.name == "Arm L")
{
//Set its new scale
gameObj.localScale = new Vector3 (10, 10, 1);
//Get the childs Transform
Transform tempTransform = gameObj.GetChild(0);
//Get the childs position
Vector3 tempPosition = tempTransform.position;
//Set the new positon (I used a low number to test to see if would bypass the exception, nope
tempTransform.position = new Vector3(tempPosition.x + 1, tempPosition.y + 1, tempPosition.z);
}
}
}
}
Any help would be appreciated
Answer by Wolfram · Jun 01, 2012 at 06:28 PM
With gameObj.GetChild(0) you're accessing a child of the Transform without checking whether it actually has children.
if (transform.childCount > 0)
//Do something about transform.GetChild(0)
Your answer
Follow this Question
Related Questions
UnityException: Transform child out of bounds 2 Answers
Trying to destroy all children, but they don't disappear (no errors) 1 Answer
How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer
Getting transform info after branch 1 Answer
FindChild not working? (Pick up Object) 2 Answers