# Set Parent to Instantiated Objects in Unity

i am having problem in attaching parent to the instantiated objects. I have attached the code below. please tell me where i am doing wrong. Thank you.

public GameObject brick;
public Transform BrickHolder;
//public GameObject Bricks;

void Start(){
for (float y = 2.16f; y < 4.88f; y += 0.34f){
for (float x = -2.125f; x <= 2.125f; x += 0.85f){

GameObject Bricks = Instantiate(brick, new Vector3(x, y, 0), Quaternion.identity) as GameObject;

if (Bricks != null) {
Debug.Log("Hello");
Bricks.transform.SetParent(BrickHolder);
}

}
}
}

• maybe BrickHolder is null. Where do you istantiate it? test if it is null. – dnk drone.vs.drones Sep 23 '15 at 6:12
• i created an object and attached it to brickholder. its isn't showing any null now but parent isn't getting assigned. – penta_d Sep 23 '15 at 7:29

I have personally never used SetParent(). I have always used the parent property, like so:

Bricks.transform.parent = BrickHolder;


This should be pretty much equivalent though, so I don't expect that is the issue (confirmed in comments already). The difference is that SetParent() allows you to update the child transform in relation to the parent, or not (with the second boolean parameter: worldPositionStays).

Update:
If BrickHolder is null, Unity will happily accept it without error, since it's valid to unparent a child object. Make sure that BrickHolder has been assigned.

Update2:
If Bricks is also null, that would suggest that brick is not valid (probably also null). You will need to make sure both brick and Brickholder have been assigned to your component in the inspector or through another script.

The only other suggestion I can offer is that sometimes you need to reset your component (the one in your question) in the inspector or remove and reattach. Sometimes Unity doesn't update the serialized values if it thinks they haven't changed, so may be referencing invalid objects.

• I've just tried both but still couldn't resolve the issue :( – penta_d Sep 23 '15 at 6:03
• Sorry, just noticed that you have BrickHolder defined as a Transform in the code of your question which means the first part of my answer is incorrect. I should ask, is BrickHolder null? You didn't indicate if Unity is producing any errors, so it's difficult to guess what the problem may be. – Fuzzy Logic Sep 23 '15 at 6:12
• [DEL] Sorry, late comment. – Exerion Sep 23 '15 at 6:17
• Its ok. Yes, BrickHolder is Null and also Bricks is showing Null While Debugging. I'm attaching the image. i.imgur.com/pBNczVs.png Unity is showing no errors. – penta_d Sep 23 '15 at 6:20
• @penta_d I've updated my answer. If none of those suggestions work, then I'm at a loss. – Fuzzy Logic Sep 23 '15 at 6:54

The only way I could replicate the issue your having is by the "BrickHolder" variable being unassigned. I would make sure that it is assigned in the inspector.

• just tried... no luck.. – penta_d Sep 23 '15 at 6:47

No need to add an assignment or setter call to establish the parent relationship. Simply, specify the fourth, parent argument for Instantiate, which, if supplied, needs to be a Transform. See it in the Unity docs.