Search Unity

Instantiate Inside Another Object [SOLVED]

Discussion in 'Scripting' started by kessler, Sep 5, 2010.

  1. kessler

    kessler

    Joined:
    Aug 1, 2010
    Posts:
    51
    I was skulking around the forums looking for help with doing this and couldn't find it. I just want to instantiate an object inside of another object in my scene instead of the usual root of the hierarchy.

    Code (csharp):
    1.  
    2.  
    3. function groundMaker ()
    4. {
    5.     if (groundPiecePos.position.x < -10)
    6.     {
    7.         Destroy(gameObject);
    8.  
    9.         var groundPieceInst = Instantiate (groundPieceClone, Vector3(0,0,0), Quaternion.identity);
    10.     }
    11. }
    12.  
    13.  
    this is what I have right now. I want to jam these instantiated ground pieces into an object I have in my scene called "world". World is continuously scrolling giving the illusion that the playing is moving WOW!!!!! hahah. OH god I am such a boob noob. Please help with my condition. THanks!
     
  2. Overunity3d

    Overunity3d

    Joined:
    Sep 5, 2010
    Posts:
    14
    I have the same problem.
    When you run the game and an instantiate occurs you will see the prefab copy stands alone. Now while the game is running go to inspector and drag that clone into the parent object. You will see the the prefab moves with the parent.

    How does one do this in code????????????


    Arg Matey! There be giants!

    Detailed technical:
    I fire a bullet. My firing vectors are not in the world object. But the object passes through the world on its own coords and vectors. It keeps going straight even though I spin the world. In other words when I spin I can spin to locate a target after I fire. This would a be good laser. But I want a bullet after I fire but the trajectory follows my line of sight. The bullet keeps going forward from my point of view.
    The instantiation prefab is at the root of the game hierarchy not at the same level as the spawnpoint.
    Do I need to copy the world vectors to the prefab vectors in the update function of the prefab? I think I just answered my own question. Anybody else?
    Or can I instantiate a prefab into any place of the hierarchy so that it follows the parent?

    Again the prefab never follows the world vectors because the instantiation is at the different level of the hierarchy and was instantiated with its own private vectors and quaternions. Correct?
     
  3. VonCede

    VonCede

    Joined:
    Feb 17, 2010
    Posts:
    45
    It's a bit hidden but tranform has a parent.

    Code (csharp):
    1. function groundMaker ()
    2. {
    3.   if (groundPiecePos.position.x < -10)
    4.   {
    5.     Destroy(gameObject);
    6.  
    7.     var groundPieceInst = Instantiate(groundPieceClone, Vector3(0,0,0),Quaternion.identity);
    8.     groundPieceInst.transform.parent = world.transform;
    9.   }
    10. }
    11.  
     
  4. kessler

    kessler

    Joined:
    Aug 1, 2010
    Posts:
    51
    thanks VonCede! I see whats going on there now. I am getting the error that its an unknown identifier for "world" even though my object is named world.

    Cheers VonCede.
    :)

    EDIT: failed. I just didnt drag and drop the world on to its variable in the IDE. Works now.

    Thanks again VonCede.!!!!!
     
  5. Overunity3d

    Overunity3d

    Joined:
    Sep 5, 2010
    Posts:
    14
    Mucho grace' :D
    Got mine working too.
    On to the next one!

    Thank you all much.
     
  6. kessler

    kessler

    Joined:
    Aug 1, 2010
    Posts:
    51
    TEAM WORK!!!