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

Resolved How to get a prefab and instantiate it in a script

Discussion in 'Scripting' started by Killercarrot102, Sep 2, 2023.

  1. Killercarrot102

    Killercarrot102

    Joined:
    Jun 20, 2022
    Posts:
    10
    dont say use
    public GameObject prefab;
    and then set it in the editor i need something that is only in the script also idk if im unsing it wrong but resouces.load wond work
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,210
    Start with the docs on Resources.Load<T>()

    It has very specific steps and requirement and I will not be retyping them for you here.

    Alternately look into Addressables, but those are VERY complicated.
     
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'm not sure about anyone else, but that's exactly what I do. I setup a class to house all my prefabs, and then when needing them through code, I just simply reference that class and the particular prefab. Which can also be done in another prefab, mind you..

    But if you're bent on using a resources folder, then read this post:
    https://discussions.unity.com/t/how-to-instantiate-prefab-with-resources-load/61785