#pragma strictfunction Awake () {//Loading a prefab resource must be resources.loadload () under the Resources folder;//must be instantiated after loading gameobject.instantiate ( );//Add Component AddComponent () to object,//find game object Find ();//get component Getcomponent (); var pprefab:gameobject = Resources.load ("prefab/ Scence ", typeof (Gameobject)) as gameobject;//loads a prefab if (null! = Pprefab) {var ppreabinstance:gameobject = Gameobject.instantiate (Pprefab);//example of if (null! = ppreabinstance) {ppreabinstance.name = "prefabscence"; var pscript: Prefab_test = Ppreabinstance.addcomponent ("Prefab_test") as prefab_test;//add components to the object if (pscript = = null) {Debug.Log (" Component add error! ");}} Else{debug.log ("Prefab Instance error!");}} Else{debug.log ("Prefab load error!");}} function Start () {var pmygameobject:gameobject = Gameobject.find ("prefabscence");//find game object if (null! = Pmygameobject) { var pscript:prefab_test = pmygameobject.getcomponent ("Prefab_test") as Prefab_test;//get component if (null! = pscript) {PS Cript. DoSomething (); } else {Debug.Log ("Get Component error!"); }} Else {Debug.Log ("Find gameobject error!");}}
Code for the Script component
#pragma strictfunction Update () {var fangle:float= 30.0f;transform. Rotate (transform.up * time.deltatime * fangle);} function dosomething () {Debug.Log ("Wo shi da huai dan!");}
Unity3d the path of cultivation: Load a prefab, add components to the object, and then find the object to get the component.