Basic syntax for unity scripts

Source: Internet
Author: User

The basic callback method

    • Strat () method: is called when the game scene is loaded, and in this method you can write some code such as the initialization of the game scene.
    • Update (): called before each frame is rendered, most of the game code is executed here, except for the physical part of the code.
    • Fixedupdate (): This method is called once at a fixed physical time. is also where the basic physical behavior code executes.

class using the awake or Start method.

The Awake method is run when the scene is loaded; the Start method is called before the update or Fixedupdate method is called the first time, and the Awake method runs before all the start methods.

Collaborative program (Coroutines)

The return value type is IEnumerator type

IEnumerator Somecoroutine () {   yieldreturn0;   wait for 1 frames   yieldreturnnew waitforseconds (2) ;  wait 2s}

Script Common operations

voidUpdate () { This. Transform. Rotate ( -,0,0, Space.world);//rotates around the x-axis relative to world coordinates     This. Transform. Translate (0,0,1);//moving Objects forward 1 units per frame     This. Transform. Translate (0,0,1, space.self);//relative to the self axis     This. Transform. Rotate ( -*time.deltatime,0,0,);//constant rotation around the x-axisVector3 a=gameobject.transform.positon;//Get Object Position coordinatesA.Y + =5*time.deltatime;//up to 5 units per second along the y-axisGameobject.transform.positon=a;//set a new location coordinate

Coordinates in Unity

The axis of the x-axis is red, and the y-axis is green to indicate the upper and lower. The z-axis is blue for front and back

Accessing Game Object Components

Getcomponent<> ();//Get Components

Transform. Find ("");//Get Sub-object

Transform.parent.Translate (0,0,1);//Find the Parent object and move it


foreach (Transform child in Transform) {//Loop get all sub-objects

Child. Translate (0,5,0);

}

Get an object by name or tag

Gameobject name =gameobject.find ("Somename");//Get a game object named Somename

Gameobject tag =gameobject.findwithtag ("Sometag");//Get a game object labeled Sometag

Getting an object by passing parameters

void Ontriggerstay (Collider other) {

if (other). Getcomponent<rigidbody> ()) {

Other. Getcomponent<rigidbody> (). Addforce (0,0,2);}}

Get an object from a component name

void Start () {

Test test=findobjectoftype<test> ();//Gets the first Test component found

Debug.Log (test.gameObject.name);//Print the name of the object that has the test component attached

Test[] tests=findobjectsoftype<test> ();//Get all the found Test components

foreach (Test A in tests) {

Debug.Log (a.gameobject.name);//print the names of all objects that have the test component attached

}}

Instanced Game Object Instantiate (Gameobject,transform.position);

Basic syntax for unity scripts

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.