Changing parent at runtime problems

Hi!

EDIT: Well, the problem is that the game object scales down to 0, not a rendering problem. I’m not the only one having this problem (can we consider it an Unity issue?). If I reset the scale to the one the game object should have everything is solved. But is there any way to prevent this? Should I change the parenting procedure to anything else to not changing scale on the process?

I’m trying to change a GameObject parent during runtime. I can do it, but the problem is that the GameObject visually disappears (it stills exists).

Here is my code where I create it (from a Object pool):

public static SlimeController NewSlime(Transform parent = null, Vector3 position = default(Vector3), Quaternion rotation = default(Quaternion)) {
		
		SlimeController results = Instantiate(Instance.slime);

		Transform transform = results.GetComponent<Transform>();
		transform.parent = parent;
		transform.localPosition = position;
		transform.rotation = rotation;
		
		return results;
	}

After this, it is now a child of the specified parent object.

This is the code where the Slime is parent-switched (inside SlimeController script):

public override bool Lock (HexController hex) {
	bool success = base.Lock (hex);

	if (!success) { return false; }

	Transform transform = this.GetComponent<Transform>();
	transform.parent = this.hex.transform;
	transform.localPosition = default(Vector3);

	return true;
}

Now the slime parent is the desired one, even its at the specified location (which is the same of the parent), but the slime disappears (visually). It still exists, I can see it at the Hierarchy tab, select it and keeps behaving as expected.

Any ideas of what i’m doing wrong?

Thanks a lot!

That doesn’t work. A child always inherits the scale of it’s parent objects. When you parent a child to a gameobject Unity tries to adjust the childs scale so the original scale is preserved. For example if the parent has a scale of (0.2, 0.2, 0.2) and the object you want to make a child has a scale of (1, 1, 1), Unity will rescale the child once it’s parented to (5, 5, 5) which will result in a world scale of (1, 1, 1).

However when you set the scale to 0 it is impossible to compensate. You basically reduced the client space to 0. You can’t even preserve the world position of the childs since the client space is zero.

There’s no way around either resetting the scale or not parenting the object to that parent. Maybe you can use a “wrapper” parent that will have both objects as childs? So you can scale one of the childs and leave the other alone.