Categories: Unity, C #, VS2015
Date Created: 2016-04-16 I. INTRODUCTION
In the game development process, the script not only needs to access the components of the game object where the script is located, but also often needs to access and control the real objects of his game. In addition, depending on your project needs, you may also need to create objects from scripts.
The end result of this example is as follows:
Second, find the game object in the scene
In unity scripts, developers can access game objects in the following ways.
1. Find by name string
Use the function Gameobject.find () to find the game object specified in the parameter. If a game object with the specified name exists in the scene, a reference to the object is returned, otherwise null is returned. If there are multiple objects with duplicate names, the method only returns a reference to the first object.
For example:
Private Gameobject obj;
void Start ()
{
Look for a game object named cube in the entire scene and assign the obj variable
obj = Gameobject.find ("Cube");
}
2, through the tag (tag) to find
Use the function Gameobject.findwithtag () to find the game object specified in the parameter. If a game object with the specified label exists in the scene, the object's reference is returned, otherwise null is returned.
For example:
Gameobject Player=gameobject.findwithtag ("Player");
3. Issues of attention
It is time-consuming to get functions for a game object or component, so instead of getting those objects in the update () method, you should find and save them in the start () method to the declared field variables, and then use those components or objects in the update () method. Third, create the game object
In addition to finding game objects in your scene, you can also create objects directly from a script and add them to your scene. See the example that follows for specific usage.
Augmented reality AR projects in real-world projects, including game projects, VR projects 、...... ), objects that are dynamically added, modified, or deleted in these scenarios need to be controlled by scripting. Iv. Design steps for the example in this section
1. Add a subfolder named "6.3" under the Assets folder in Unity's project and create a scene under the folder named "Scene6_3.unity".
2. Add a plane to the scene, set (reset) its position:
3. Add a cube to the scene.
4, adjust the camera and cube parameters, make it easy to see:
The parameters of cube are as follows:
The parameters of Main camera are as follows:
5. Add an empty gameobject to the scene and replace it with the name manager:
6. Switch to VS2015 and add a script file named "Demo3_1.cs" under 6.3 sub-folders:
The results you see after adding are as follows:
7. Change the Demo3_1.cs file to the following content and save:
usingUnityengine;usingSystem.Collections; Public classdemo3_1:monobehaviour{ PublicTexture Texture; PrivateGameobject obj; PrivateRenderer render; voidStart () {//get the Game objectobj = Gameobject.find ("Cube"); //gets the renderer for the objectRender = obj. Getcomponent<renderer>(); } voidOngui () {if(Guilayout.button ("Create a cube", Guilayout.height ( -))) { //set the model default to CubeGameobject obj =gameobject.createprimitive (Primitivetype.cube); //add a rigid body to the object, giving the physical propertiesObj. Addcomponent<rigidbody>(); //the material that gives the object is redObj. Getcomponent<renderer> (). Material.color =Color.green; //set the name of the objectObj.name ="Cube"; //set the position coordinates of this model materialObj.transform.position =NewVector3 (0, 5f,0); } if(Guilayout.button ("Create a Sphere", Guilayout.height ( -))) { //set the model default to CubeGameobject obj =gameobject.createprimitive (Primitivetype.sphere); //add a rigid body to the object, giving the physical propertiesObj. Addcomponent<rigidbody>(); //the material that gives the object is redObj. Getcomponent<renderer> (). Material.color =color.red; //set the name of the objectObj.name ="Sphere"; //set the position coordinates of this model materialObj.transform.position =NewVector3 (0, 5f,0); } if(Guilayout.button ("Add Color", Guilayout.width ( -), Guilayout.height ( -))) { //modify render color to redRender.material.color =color.red; } if(Guilayout.button ("Add a decal", Guilayout.width ( -), Guilayout.height ( -))) { //Add a component mapRender.material.mainTexture =texture; } }}
8. Switch to unity and drag and drop a picture to 6.3 sub-folders (just pick a picture on the line):
9. Select the manager in the hierarchy view, then drag and drop the demo3_1 onto the view, then drag and drop the picture onto the texture (assigned initial value) under the View script:
10, press the "Play" button to enter the preview mode, you can see the following running interface:
Click the "Create Cube" button to see the Falling cube (because a rigid body is added to the code so it will fall automatically), but when it falls on the plane, it stops:
Click the Create Sphere button and the ball will drop to the top of the cube to stop:
Click the Add Color button to change the color of the cube on the left:
Click on the "Add texture" button to map the cube to the left:
Haha, it's fun.
"Unity" 6.3 Create and Access game objects through C # scripts