Does transform.root work when a parent is made the child of another object through script? It isnt working for me.
In one script I create a weapon, "item" with a child and a child of that child, which has a script that is used to detect collisions:
Transform item =(Transform)Instantiate(itemPrefab, itemPosition.transform.position, itemPosition.transform.rotation);
item.parent = itemPosition.transform;
On the child of the child of "item", the script which is used to detect collisions has this code:
Transform playerTransform;
void Awake () {
playerTransform = transform.root;
}
But transform.root returns only "item," not the parent of the parent of the parent... of item, which I though would be the root, and which I am trying to access. Doing a search for the tag of the very first parent won't work because I will have multiple players that all have this sort of
item.
Is there any way to access the very first parent? I've also tried "player transform = transform.parent.transfrom.parent.transform.parent etc...", but that returned a null reference exception when I started accessing parents of "item."
↧