I am having a hard time getting the child of an instantiated object to scale differently than its parent (i’m working on healthbars). I want my child object to stay at the same scale/length at all times, while the parent object is able to change length.
I’ve tried attaching scripts to my parent object, and trying to manually change the localScale.x of the child object every update. That doesn’t seem to work. I’ve also tried attaching a script onto the child object itself, and setting its localScale.x every update, but that didn’t work either.
It seems no matter what I do, the scale of the child always reflects the scale of the parent (even though if I look at the “x” scale of the child object, it always stays the same)…but that’s what parenting is supposed to do I guess.
So here are some of the variations of scripting I’ve tried. I’ve tried attaching this script to the parent object:
#pragma strict
private var thisTransform : Transform;
private var healthBarChild : Transform;
private var healthpercent : float;
function Awake () {
thisTransform = transform;
healthBarChild = thisTransform.Find("Healthbar4");
//healthBarChild.transform.lossyScale.x = 0.1987;
}
function Start () {
//thisTransform = transform;
//healthBarChild = thisTransform.Find("Healthbar4");
}
function Update () {
healthBarChild.transform.localScale.x = 0.1987;
}
And I’ve tried a variation of this script that doesn’t need to grab the child and just changes the child’s transform directly.
I should also mention, what I"m doing is on “Start”, I instantiate a health bar prefab above several hundred unit’s heads. So I’m working with quite a few game objects. And then via another script, I’m changing the parent healthbar’s length relative to it’s health percentage.
All in all, I’ve tried quite a few methods to break the parent-child scale relationship, and it never seems to break it.
if you scale a parent, it will scale the child, theres no avoiding that
if possible, instead of having one a child of the other, have both as children of a third, never-scaled object.
If the parent-child relationship is required, an alternative is scaling the child by (1/parentScale) (so if parent scale is 2, child scale is 0.5)
Hmm that’s a good idea to have two childs on a never-scaled parent. I think I might try that.
Thanks for the ideas hpjohn. I still really wish I could just break the parent-child relationship on at least one or two things, like scale or rotation. I’ve actually already gotten the effect I"m going for, but it requires maintaining the position of about 150 extra gameobjects every update.
So I’m trying to optimize it. It’s not really game-breaking, but it adds a lot of extra cpu load, causing my fps to drop down below the threshold I want it to be at. Making both gameobjects the child of a parent (non-scaled) object might work, so I’m gonna try that
Just to confirm to me this is the best solution. Like avoiding it if possible as suggested. Also remember that sometimes you might inadvertently attempt to resize the parent object so that its collider might fit a particular size / orientation forgetting that the collider is something you could just resize separately from the actual parent object if not a mesh collider
Best way is multiply the current localScale with a value.
Eg. If you want to double the size of the current size
transform.localScale = new Vector3(transform.localScale.x / 2f, transform.localScale.y / 2f, transform.localScale.z / 2f);