Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Create a Prefab?

Discussion in 'Scripting' started by JVGameDev, Mar 19, 2017.

  1. JVGameDev

    JVGameDev

    Joined:
    Oct 5, 2015
    Posts:
    137
    Sorry, I don't know why I couldn't find this on the docs, but what function do I use to create a prefab at a certain coordinate?
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. JVGameDev

    JVGameDev

    Joined:
    Oct 5, 2015
    Posts:
    137
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Normally you create a prefab of the object. Then you have to fetch a reference to that object. (usually a public variable and drag and drop works fine) and this will instantiate it. You can also look at object pooling, depending on how your game function.

    For instantiate, it doesn't have to exist in the scene.
     
  5. clarklak11

    clarklak11

    Joined:
    Aug 26, 2016
    Posts:
    22
    You need to create first a bullet, then make it a prefab. Then you can Instantiate the bullet even it is not on the scene.
    Code (CSharp):
    1. public GameObject bulletPrefab;
    then drag the bullet prefab to the variable bulletPrefab.

    now you can Instantiate it with this

    Code (CSharp):
    1. GameObject bullet = (GameObject)Instantiate (bulletPrefab, transform.position, transform.rotation);