Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiate a prefab next to another. HELP PLEASE

Discussion in 'Scripting' started by SirMarley, Aug 9, 2014.

  1. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    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

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D ground)
    2.     {
    3.         if (ground.gameObject.name == "creationPoint")
    4.         {
    5.             Instantiate(brick1, new Vector3((brick2.x + 13.59f), (brick2.y + 0.1888445f), 0), Quaternion.identity);
    6.         }
    7.     }
    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!!!!
     
  2. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    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);

    2. "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 :
    Code (CSharp):
    1. void Start()
    2. {
    3.        lastBrick1Pos = brick1.transform.position;
    4.         lastBrick2Pos = brick2.transform.position;
    5. }
    6.  
    7. void OnTriggerEnter2D(Collider2D ground)
    8.     {
    9.         if (ground.gameObject.name == "creationPoint")
    10.         {
    11.             GameObject b = Instantiate(brick1_prefab, new Vector3((lastBrick2Pos .x + 13.59f), (lastBrick2Pos .y + 0.1888445f), 0), Quaternion.identity) as GameObject;
    12.          
    13.             // then store new info
    14.             lastBrick1Pos = b.transform.position;
    15.  
    16.             // same if create brick2 next to brick1
    17.  
    18.         }
    19.     }

    Hope this works!
     
    SirMarley likes this.
  3. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Dylan.... that was excelent. It works perfect. Thanks a lot!!!!!
     
  4. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    No problem!