Instantiate by name, without editor linking, is it possible?

Hi
all the tutorials that I have seen about using Instantiate, refer to the entity that is created by code with a link between the Prefab and the scrip in the Editor (drag and drop) using a public property Transform at the script.
Is it possible to avoid the editor linking and call the instance directly?

Instantiate("myPrefabName", ...)

I recall seeing something like this days ago but didn’t put much attention, and I can’t find it again, maybe I was wrong!

thanks!

EDIT: also sorry for posting here, can this thread be moved to Script? I can not move it neither delete it.

if they are in the resources folder you can load them from there by name

thanks by don’t recall how…
some function to look for?

sorry wrong one. this one

What is your rationale for wanting to search for it at runtime?

@handsomePATT:
thanks for the reply! sadly it’s not working, maybe something basic I’m missing here:

using UnityEngine;
using System.Collections;

public class Wall : MonoBehaviour {
    private Transform brick;
    public  Transform thing;
    void Start() {
        brick = (Transform)Resources.Load("Brick"); // this returns NULL, tried also with "Brick.prefab", the file exists in Assets folder
        thing = (Transform)Instantiate(thing, new Vector3(2.5f, 2.6f, -4.4f), Quaternion.identity); // this works if thing is linked in the editor
    }
}

@Jessy:
I want to create my levels dynamically

your prefab needs to be in the Resources folder

I don’t think that’s enough information. It sounds to me like you just don’t know how to collect references in an easy-to-deal-with way. More code may help; from the above, it’s not obvious why you can’t store what a brick is.

Hi Jessy,
first many thanks for taking your time in my question :smile:
Of course I can store references to 2 o 3 different entities (I’m planning this not for bricks but for NPC’s)
the trouble is that this approach is not scalable, I want a programmatic way to create entities at runtime, kind of a NpcFactory

almost there, after putting the prefab in the Resource folder:

using UnityEngine;
using System.Collections;

public class Wall : MonoBehaviour
{
    private GameObject brick;
    public  Transform thing;

    void Start()
    {
        brick = (GameObject)Resources.Load("Brick");
        print(brick);
        brick.transform.position = new Vector3(2.5f, 2.6f, -4.4f);
        thing = (Transform)Instantiate(thing, new Vector3(2.5f, 2.4f, -4.4f), Quaternion.identity);
    }
}

a game object is created, but it is not visible, any ideas?
it must be near the other brick that I created with Instantiate() that is working ok…
thanks!

finally! :smile:

using UnityEngine;
using System.Collections;

public class Wall : MonoBehaviour
{
    private GameObject dynamicBrick;

    void Start()
    {
        dynamicBrick = (GameObject)Instantiate(Resources.Load("Brick"), 
                                              new Vector3(2.5f, 2.6f, -4.4f),
                                              Quaternion.identity);
    }
}

Thanks to all for their tips