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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    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,146
    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);