Now that you know how to control the motion of an object, continue to expand and learn how to conveniently control multiple objects at the same time.
Access other objects in the script
I. Script Parameters
Public GameObject gameObject;
Then you can see the "public" parameter in the control panel of the script, and drag the object to the parameter of the script to implement control.
Generally, we can control multiple objects in a script, and then assign the script to permanent objects such as the "Ground" to facilitate control and share the script with others ).
Ii. Object Name
Add a ball and Cube named Capsule and Cube.
GameObject capsule = GameObject. Find ("Capsule"); // Find the object name. If two objects have the same name, only the top one is obtained. GameObject cube = GameObject. Find ("Cube"); capsule. renderer. material. color = Color. red; cube. renderer. material. color = Color. gray;
// You can use the directory method to obtain a more specific location.
For example
A1/C
B1/C
Two groups of objects. GameObject cube = GameObject. Find ("/A1/C"); you can access the following C object
III. Statement block parameter transfer
GameObject n; // Collision trigger event (manually enter the code and the function name cannot be changed) void OnCollisionEnter (Collision collisionInfo) {n = collisionInfo. gameObject; // obtain the collision object n. renderer. material. color = Color. red ;}