I am trying to find the parent of a prefab object (a custom class that inherits from Toggle) dynamically doing this :
var parentObject = this.transform.parent;
However, parentObject is returned null, despite the parent object existing and being interactable in the UI. Anyone knows what the reason could be? Am I doing it wrong?
I also tried running the following code :
if (this.transform.IsChildOf(transform))
{
Debug.Log("Object" this.ToString() " has parents");
}
Which returns "Object myPrefab (CustomToggle) has parents"
CodePudding user response:
You get prefabs from the project, they can not have any parents. The Prefab's themselves are the head's, and when you call them from the asset folder, they will have no parents. Consider that if a prefab is the child of another prefab, getting it in the inspector has nothing to do with other prefabs, The reason for displaying the log is that each object is also a child transform.
if (transform.IsChildOf(transform))
{
Debug.Log("the transform is child of itself.");
}