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 many gameobject from prefab with one script in one scene

Discussion in '2D' started by ramose, Feb 9, 2015.

  1. ramose

    ramose

    Joined:
    Mar 15, 2014
    Posts:
    9
    I have three prefabs : A,B,C

    How to instantiate those three in one script with something like this:

    createObject(A) ?

    I know that we have to create public variable in script, then drag the prefab into that script component. But what if the prefabs to instantiate is various.
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    If you expose a variable in your script, e.g. in unityscript: var objecta:GameObject; . ... then in the inspector you can drop a prefab on that object, and then in the script you can `instantiate` that prefab.
     
  3. ramose

    ramose

    Joined:
    Mar 15, 2014
    Posts:
    9
    Thanks for reply, imaginaryhuman :)

    In most cases, I need to instantiate different prefab at runtime, with the same script. How can I do this?
     
  4. ramose

    ramose

    Joined:
    Mar 15, 2014
    Posts:
    9
    I found this way:

    Instantiate(Resources.Load ("hero1", typeof(GameObject))) as GameObject;

    is this the right way?