1
\$\begingroup\$

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);
            }

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

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.

\$\endgroup\$
  • \$\begingroup\$ I've just tried both but still couldn't resolve the issue :( \$\endgroup\$ – penta_d Sep 23 '15 at 6:03
  • \$\begingroup\$ 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. \$\endgroup\$ – Fuzzy Logic Sep 23 '15 at 6:12
  • \$\begingroup\$ [DEL] Sorry, late comment. \$\endgroup\$ – Exerion Sep 23 '15 at 6:17
  • \$\begingroup\$ 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. \$\endgroup\$ – penta_d Sep 23 '15 at 6:20
  • \$\begingroup\$ @penta_d I've updated my answer. If none of those suggestions work, then I'm at a loss. \$\endgroup\$ – Fuzzy Logic Sep 23 '15 at 6:54
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ just tried... no luck.. \$\endgroup\$ – penta_d Sep 23 '15 at 6:47
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.