Create objects, scripts to mount on Cube2
#regionThrough Gameobject.find (""): Find game objects//1. Find the Game object by nameobj = Gameobject.find ("Cube1"); Obj. Getcomponent<MeshRenderer> (). Material.color =color.red; //2. Find Cube1 Sub-node Cube2, find game object name can be foundobj = Gameobject.find ("Cube2"); Obj. Getcomponent<MeshRenderer> (). Material.color =Color.Black; //3. You can find the path of the game object directly, can find the game object with the same name, can distinguish by pathobj = Gameobject.find ("Cube1/cube2"); Obj. Getcomponent<MeshRenderer> (). Material.color =Color.Blue; //4. If there are several game objects of the same name under the same node, the last name of the game object in the hierarchy panel hierarchy will be found//find objects in a recursive way and return to the last object found in the hierarchy panelobj = Gameobject.find ("Cube"); Obj. Getcomponent<MeshRenderer> (). Material.color =color.red;
Summary: Gameobject.find (""): Find game objects
* 1. Can find objects of any level, not affected by the level position of objects bound by the script
* 2. You can specify a level lookup, or you can find it without specifying a hierarchy
* If you specify a level, you will find it in the corresponding hierarchy
* If you do not specify a hierarchy, you will find it in all levels (names cannot be duplicated)
* 3. If no hierarchy is specified and the object being searched is more than one object of the same name, the last object of the list in the hierarchy panel will be found
* 4. No support for finding hidden objects
1 #regionTransform. Find (); Find a Game object2 3 //scripts are mounted on Cube24 ///1. Can find sub-node game objects5 ///2. Can find the full path of the game object6 //1. Find Cube17 //Tran = transform. Find ("Cube1");//cannot find8 //Tran = transform. Find ("Cube2");//cannot find9 //Tran = transform. Find ("Cube3");//We found it.Ten //Tran = transform. Find ("/cube1/cube2"); //looked up OneTran = transform. Find ("/cube");//Find the ATran.gameobject.getcomponent<meshrenderer> (). Material.color =color.red; - - #endregion
Transform. Find (); Find a Game object
* 1. Only objects with a subset can be found
* 2. If you want to find objects at another level (sibling or parent), you need to use the full path to find
* 3. Support for finding hidden objects
Small white _unity Engine _ Access Components--gameobject.find () and Transform.find ();