Search Unity

Spawn a GameObject from another different GameObject

Discussion in '2D' started by DaRaccoon, Feb 18, 2019.

  1. DaRaccoon

    DaRaccoon

    Joined:
    Oct 28, 2018
    Posts:
    11
    Hi,
    I wanted to spawn a prefab called "Copy" when my character jumps. I searched only but i only found methods which copy a certain game object and does not create a new one, what methods should i use to do so?
    I want it to spawn in the position my player is in the moment i press the jump button, and it must be a different gameobject since it will have it's own set of scripts different from the main character.
     
  2. Geejayz

    Geejayz

    Joined:
    Feb 8, 2016
    Posts:
    73
    If you have made the 'Copy" prefab in the way that you require, you should be able to spawn it from the jump method using...

    Code (CSharp):
    1. Instantiate (Copy, transform.position, Quaternion.identity);
     
  3. DaRaccoon

    DaRaccoon

    Joined:
    Oct 28, 2018
    Posts:
    11
    what is the way i require? because the prefab is there and now it only has a sprite, but maybe i'm missing something i don't know i have to set?
     
  4. Geejayz

    Geejayz

    Joined:
    Feb 8, 2016
    Posts:
    73
    Does the sprite appear at the position you expect?
     
  5. DaRaccoon

    DaRaccoon

    Joined:
    Oct 28, 2018
    Posts:
    11
    no you didn't understand, i don't know how to correctly spawn it it's not the position
     
  6. pod11

    pod11

    Joined:
    Jan 6, 2019
    Posts:
    60
    You should make a public variable in scritp for object you want to spawn, and just drag-drop prefab into box that appear in editor (in object you placed script on).
    When you jump save transform.position before jumping, and after executing jump use instantiate(gameObjectVariable,savedPosition,transform.rotation).
    You place the script on jumping object.
    There are milions of tutorials on youtube about instantiating objects.