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