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

Initializing prefab gameobjects from project hierarchy

Discussion in 'Scripting' started by crowmagnon, Jan 14, 2013.

  1. crowmagnon

    crowmagnon

    Joined:
    Mar 2, 2011
    Posts:
    36
    I know you can initialize a GameObject with a prefab by dragging the prefab onto the script's prefab variable. But what if I have a ton of prefab variables I want to initialize automatically with un-instantiated prefabs from the project's prefab folder instead of having to drag each prefab onto it's respective variable? Kind of like a GameObject.Find function that doesn't require the object to be instantiated into the scene. Is this possible?
     
  2. Goblox

    Goblox

    Joined:
    Nov 3, 2012
    Posts:
    178
    GameObject go = (GameObject)Instantiate(Resources.Load(prefab));
     
  3. crowmagnon

    crowmagnon

    Joined:
    Mar 2, 2011
    Posts:
    36
    That does the trick. Thanks!