How do I instantiate prefab item in C# ?

i know it’s kinda of silly, but i just couldn’t figure out the correct way, here’s code I start

using UnityEngine;
using System.Collections;

public class MainChangeItems : MonoBehaviour {

//	public GameObject prefab;

	class item {
		public int type;
		public GameObject model;
	}

	public ArrayList items;

	// Use this for initialization
	void Start () {
		GameObject prefab = FindObjectOfType(typeof("coat1m")) as GameObject;
		GameObject obj;
		items = new ArrayList();
		
		obj = (GameObject)Instantiate(prefab,transform.position,transform.rotation);
		obj.active = false;

		item AItem=new item();
		AItem.model = obj;
		items.Add(AItem);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

basically, if I have an instance of prefab in the scene, there’s no problem. But if I don’t have any in the scene, how do I instantiate this prefab by script ? Thanks a ton.

Fang

Uncomment the line declaring your public ‘prefab’ variable, then comment out the one that uses FindObjectOfType. With the variable exposed drag assign the prefab as that variable’s value (select the object in the scene with your script attached then drag the prefab from the Project folder on to the variable value). Play scene, have fun. Done!

thanks, HiggyB

but i don’t want to have the prefab connection in the class. what I would like to do is kinda the following.

find the prefab by name or anything else, then instantiate it without reference it in the class.

so one thing I am not sure how to do it correctly is to find the prefab I wanted by script.

That api might be what you’re looking for then.