I have a parent with child components. I would need to move its child component to a precise location. However, whenever I move them, they either stay in their position or move to a wrong one (depending if I use transform.position or transform.localPosition). Also, when I query their location, all child components return the location of the parent. In the Inspector, all the child component locations show 0, 0, 0.
I also tried Instantiate, with no better results, any child just stays in its current location.
How could I move a child component then reliably to a location?
I would like the child (foundItem) to get moved before the player which is the examinePos. With this script (or by using transform.positions), the child is always moved to a wrong location. Also, in inspector, the Transform of the children is always 0, 0, 0 regardless where they are.
It looks like you may be misunderstanding what worldToLocalMatrix does. In general it’s very rare that you’ll actually need to use that, so unless you’re doing very precise and relatively advanced math with it it’s probably not the right solution.
So with this in mind, I’ll assume that you also tried using transform.position exactly where you have localToWorldMatrix here. First thing that comes to mind is that you say you would like the child “moved”, but you are calling Instantiate, which creates a copy of the child at the specified position. If you’re looking at the original child when that happens, it would certainly appear as if it’s not moving. Could that be it?
Dear StarManta, thank you for your continued help.
Unfortunately, that is exactly the code that I tried right at the start, with unsatisfactory results. When this is used, results are strange; a child item is placed way off the playerTarget. However, if I attach a new object as a child and this is set as foundItem, it moves to the exact location. However “native” child objects (child components that are part of the object as it was imported to editor, not attached later to it) do not, but tend to move to different locations.
This is issue is related to child behaviour somehow; none of the “native” children have a Transform Position other than 0, 0, 0. Children that are added, however have a Position that matches their accurate world location. A solution would be to make the children have an actual location somehow apart from 0, 0, 0.
To cut it short; how to get the location of a child that just show’s 0, 0, 0 as its Location.
No object in hierarchy scale is 0, 0, 0. Scale of all children is 1. The parent is 0,1. It seems when the child objects are moved, they move according to their offset from their parent, hence its impossible to move them to a position as there is no (or I can’t find) a way to get a reference to their actual world location or to the offset from the parent.
In code, transform.position should give you the objects position in world space. transform.localPosition will give you the position relative to the parent (Thats what you see in the inspector).
Hi kdgalla, thanks for the post, unfortunately this is what I tried as the first remedy and I only hope it was so easy. With code:
Debug.Log("transform position");
Debug.Log(foundItem.transform.position);
Debug.Log("transform local position");
Debug.Log(foundItem.transform.localPosition);
The debug result is same coordinates for all children, even if they are situated clearly at very different locations. So I am still struggling to get the actual location of the child object; now it just keeps displaying the same XYZ for all. The transform position coordinates are Position of the parent.
foundItem.transform.position is the actual location of foundItem in world space. If it’s not the position that you are expecting, then maybe foundItem is not the right GameObject. Did you debug.log the name of foundItem?