07 one script Controls Multiple objects in three ways-programmer learning Unity3d

Source: Internet
Author: User

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 ;}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.