- Home /
Is it possible to get a cloned gameObject's parent name?
Hey guys, right now, I can't seem to get my cloned object's parent's name.
After instantiating my object, it became a child of another object.
So, this code
if (transform.parent.name == "DropZone1")
pos = new Vector3(-0.475f, 0,0);
returns a NullReference. I'm guessing it's trying to reference its prefab, which doesn't have a parent. But it's already in the game, so it should be trying to reference it's current parent instead.
print (transform.parent.name);
Debug.Log (transform.parent.name);
both also returns a NullReference.
Anyone knows a solution to this? Thanks!
It may be a nesting error! Can you show the code that make the clone a child please? I have a theory!
It's a Drag & Drop from NGUI
void OnDrop (GameObject go)
{
DragDropItem ddo = go.GetComponent<DragDropItem>();
if (ddo != null)
{
GameObject child = NGUITools.AddChild(gameObject, ddo.prefab);
Transform trans = child.transform;
trans.position = UICamera.lastHit.point;
Destroy(go);
}
}
NGUITools.cs
static public GameObject AddChild (GameObject parent, GameObject prefab)
{
GameObject go = GameObject.Instantiate(prefab) as GameObject;
if (go != null && parent != null)
{
Transform t = go.transform;
t.parent = parent.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
go.layer = parent.layer;
}
return go;
}
Answer by raimon.massanet · Nov 29, 2013 at 02:33 PM
I am assuming the code that's giving you a NullReference
is attached to the instantiated object. So, when are you executing that code?
Maybe you are executing it in Awake
which is being executed before AddChild
has parented the new object to the desired parent?
omg... you're right!
Because Awake is calling this method, so I didn't notice it and thought it's somewhere after Awake... Thanks man! and Steve for trying to help ^^
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 2 Answers
how to change the Parent of an object to the current object that a script is on? 1 Answer
Search for an object by name null reference exception 1 Answer
Parenting an instantiated Prefab... again 1 Answer
[SOLVED] How to make to clone an object and make it parent of the transform? 1 Answer