1. Create game objects
Gameobject. createprimitive (). The parameter is the system's default sphere, cube, and other game objects.
Addcomponent () to add components for game objects.
ProtectedGameobject OBJ;//Use this for initializationVoidStart () {OBJ=Gameobject. createprimitive (primitivetype. cube); obj. addcomponent ("Rigidbody");//Add a rigid body PropertyOBJ. Name ="Cube"; Obj. Renderer. Material. Color=Color. Red ;}
2. Get game objects
You can get a single object by name or a single or group of objects by TAG.
(1) Use the find () method to pass in the complete path name of the object to obtain the object.
OBJ = gameobject. Find ("cube"); // if it is a subfile, the format is as follows:"Objs/sphere/cube"
OBJ. addcomponent ("res"); // Add a script
OBJ. Renderer. Material. Color = color. Yellow;
OBJ. Renderer. Material. maintexture = resources. Load ("021714386688207") as texture; // The loaded resource cannot contain a suffix.
(2) Use the findwithtag () method to obtain the tag object. The parameter is the tag name.
OBJ = gameobject. findwithtag ("Tagsphere"); Obj. Renderer. Material. Color= Color. Yellow;
(3) findgameobjectswithtag () to obtain a group of objects.
ProtectedGameobject [] objs;VoidStart () {objs= Gameobject. findgameobjectswithtag ("Tagsphere");Foreach(Gameobject oInObjs) {o. Renderer. Material. Color=Color. Blue ;}}