Unity Scripting System

Source: Internet
Author: User

What is a script? The script is a monobehavior, and the inheritance relationship is

Monobehavior, Behavior, Component, Object

The behavior of Gameobject is controlled by the components attached to them.

Game Events

Monobehavior

The class name and script name must be the same

Public class Mainplayer:monobehaviour {

public string MyName;

Use this for initialization

void Start () {}

Update is called once per frame

void Update ()

{ }

}

There can be no constructor function. The creation of an object is created by the editor and is not created at the beginning of the game.

This My name, is the editor automatically generated according to your variable name, just to display it.

getcomponent function: The script itself is a component, calling the Getcomponent function is another component of the same genus Gameobject object, which is to take the sibling component.

So what does it mean to use the transform variable in a script? Is transform = Getbelonggameobject (). Getcomponent<transform> ();

void Start () {

Transform.position = vector3.zero;//is supposed to get the transform component through the getcomponent () function, but because this component is so often used, it is stored directly with a variable.

The built-in script component objects can be viewed in the Monobehavior documentation.

}

Public class Enemy:monobehaviour {

public Gameobject player; Storing references to other objects in the current component

Public Transform Strans; //In the current component store variables of other component types, in which case you can assign any object containing Transform this component to this variable in the editor!

void Start () {

Start the enemy ten units behind the player character.

You can still access the player. Getcomponent ()

transform.position = Player.transform.position-vector3.forward * 10f;

}

}

Find child objects

Transform lst = new Transform[transform.childcount];

For (Transform T in Transform) {

lst[i++] = t;
}

Unity Scripting System

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.