Instantiate a prefab next to another. HELP PLEASE

Guys… Help please!!!

I have 2 bricks on the screen and also like a prefab (brick1 and brick2) and, on the brick1 I have an invisible GameObject (creationPosition) which is a trigger.

The idea is, when the player pass over this GO, another brick is created next to brick2, then another brick2 next to brick1 and so on…

Now… I have the

void OnTriggerEnter2D(Collider2D ground)
    {
        if (ground.gameObject.name == "creationPoint")
        {
            Instantiate(brick1, new Vector3((brick2.x + 13.59f), (brick2.y + 0.1888445f), 0), Quaternion.identity);
        }
    }

but, of course, “.x” does not work. (I have also tried brick2.transform.position but it did not work either)

So, what is the right way to make this happen?

Thanks a lot!!!

hey buddy!
I can only get a blurry image of your problem here…
but i hope this is at least a bit helpful!

  1. reference
    So i’m sure you have your brick1 and brick2 references set up.
    But i think the reference is pretty important cause Instantiate(brick1, new Vector3((brick2.x + 13.59f), (brick2.y + 0.1888445f), 0), Quaternion.identity);
    brick1 here is a prefab, but brick2 here should be the object which is already in your scene(the one you want to create birck next to), not the prefab ,so you can access the transfor.position.x;
    Creating brick2 next to brick1 is the same.
    Instantiate(brick2, new Vector3((brick1.x + **), (brick1.y + **), 0), Quaternion.identity);
    brick2 here is the prefab, but brick1 should be the brick1 in the scene already.

The variables here are getting confusing.
I suggest you to define two variables : brick1_prefab,brick2_prefab refer to the prefabs.
and another two : brick1 , brick2 refer to the bricks in the scene;

So finally it should be
Instantiate(brick1_prefab, new Vector3((brick2.x + 13.59f), (brick2.y + 0.1888445f), 0), Quaternion.identity);
Instantiate(brick2_prefab , new Vector3((brick1.x + **), (brick1.y + **), 0), Quaternion.identity);

  1. “create next to last brick”
    I’m guessing you want to create another brick next to the last brick when collide.
    If you do. Let’s see if this works:
    → we want to create a new brick next to last brick,therefore we have to store this new-created brick’s info, so we can use for next brick-creation;

let’s define a new var for storing transform information(we need position,don’t we)

private Vector3 lastBrickPos;

then use it :

void Start()
{
       lastBrick1Pos = brick1.transform.position;
        lastBrick2Pos = brick2.transform.position;
}

void OnTriggerEnter2D(Collider2D ground)
    {
        if (ground.gameObject.name == "creationPoint")
        {
            GameObject b = Instantiate(brick1_prefab, new Vector3((lastBrick2Pos .x + 13.59f), (lastBrick2Pos .y + 0.1888445f), 0), Quaternion.identity) as GameObject;
         
            // then store new info
            lastBrick1Pos = b.transform.position;

            // same if create brick2 next to brick1

        }
    }

Hope this works!

1 Like

Dylan… that was excelent. It works perfect. Thanks a lot!!!

No problem!