instantiating objects from assets

Hi there
ok, Im trying to figure out if its possible to instantiate a GameObject directly from Assets, without setting a reference to it in the inspector.

Thanks !

Create a Resources folder in your Assets. Put your prefabs under there. In code, do this:

JS

var objPrefab : GameObject = Resources.Load("objPrefab");
var obj : GameObject = Instantiate(objPrefab);

C#

GameObject objPrefab = Resources.Load("objPrefab") as GameObject;
GameObject obj = Instantiate(objPrefab) as GameObject;

I hope this helps.

Thanks! Would this work for publishing online ?

I’m pretty sure it does. Simply build for web and give it a try :smile:

I’ll give it a try :slight_smile: