- Home /
how to instantiate object with content prefab and not him self
first we instantiate GameObject somehow:
public GameObject Prefab ;
Instantiate(Prefab) as GameObject ;
than we came along that Item and we try to pick it up
public GameObject Prefab ;
public GameObject Player ;
public Inventory InventoryScript ;
void Start () {
Player = GameObject.Find("Camera");
InventoryScript = Player.GetComponent("Inventory") as Inventory;
}
void OnMouseDown () {
InventoryScript.GivePrefab ;
Destroy(gameObject);
}
and in inventory GivePrefab we have content Prefab Object IF we comment Destroy(gameObject)
and how do I tell prefab item that his prefab GameObject is not him self but prefab?
it's no problem for Object that I place in scene my self as I edit the object and give it Prefab
but problem is how do I tell object that it's himself Prefab and not him self (new made)
it's kinda hard to explain
or maybe easier how do I tell what's prefab with code and not with editor?
Answer by sdgd · Jan 29, 2013 at 03:09 PM
Question was solved with different putted question how to instantiate prefab with Resources.Load
GameObject instance = Instantiate(Resources.Load("Hatchet")) as GameObject;
in order that Object won't be null it has to be in Project "Resources" Folder
if it doesn't work
"Resources" Folder must be created manually with Explorer (TotalCommander) in:
Default: c:\Users\UserName\Documents\New Unity Project\Assets\Resources\` for custom folder just search for
New Unity Project\Assets\Resources`
I for example have it at:
c:\Programming\unity projects\crafting house\Assets\Resources\
Answer by cdrandin · Jan 28, 2013 at 03:58 AM
// Declare in globals public GameObject object;
// Drop prefab into the object location in the inspector in Unity's editor. When that is done,
Instanitate(object, someplace.position, Quaternion.identity);
// Which creates an object based off the prefab.
yes I'm telling that that creates me a item with content him self and not prefab
yeah name is same but when I delete that object as (pickup) game object doesn't exist any longer
so how do I tell PREFAB that he needs to have a PREFAB in himself when instanced and not himself ins$$anonymous$$d
IF I give Prefab manually in scene and drop prefab in to him GameObject isn't deleted but that's not instance it's $$anonymous$$anually adding to scene
I don't want to be in game near every player and instance objects ins$$anonymous$$d of them just so it'll have a prefab and not game object him self
I know it's hard to explain
IF I want that it would work I would have to create 2 same Prefabs
let say Prefab A, B (both same size everything same) Except
Prefab A is calling Prefab B for a prefab GameObject Prefab B is calling Prefab A for a Prefab GameObject
that's the only way that I would work the way you describe but that's way too BIIIIIIIIIIG Inefficiency
because IF I call it like
Prefab A is calling Prefab A for Prefab GameObject
once it'll be Instantiate Prefab A won't call for Prefab A any longer but it'll call Him self as GameObject Witch can be deleted and I don't want to delete the
Public GameObject Prefab ;