This is an old revision of the document!
Creati un script C# care la rulare sa:
Gasiti pe MS Teams
In Unity scripting, there are a number of event functions that get executed in a predetermined order as a script executes. This execution order is described below:
Difference between Start and Awake Unity Events
Awake as ” initialize me” and Start as ” initialize my connections to others.” You can't rely on the state of other objects during Awake, since they may not have Awoken themselves yet, so it's not safe to call their methods.
Start function is called only if the script component is enabled. Awake function called when the script instance is being loaded.
If the script is NOT enabled at the beginning of your game, and you don't need the variables to be initialized, start() would be saving performance as awake() would be called regardless.
Difference between Update, Fixed Update and Late Update.
Update: (Most things)
FixedUpdate:(Physics things)
LateUpdate:(After update)
Script compilation order inside Unity
There are four separate phases of script compilation. The phase where a script is compiled is determined by its parent folder. In a script compilation, the basic rule is that anything that is compiled in a phase _after_ the current one cannot be referenced. Anything that is compiled in the current phase or an earlier phase is fully available.
The phases of compilation are as follows:
A common example of the significance of this order occurs when a UnityScript file needs to reference a class defined in a C# file. To achieve this, you need to place the C# file inside a Plugins folder, and the UnityScript file in a non-special folder. Because C# code is compiled before JS code, so in general, while JS code can access C# classes, the opposite is not possible If you don't do this, an error is thrown saying that the C# class cannot be found.
Variables and Parameters
Variables can be controlled from the Unity Editor as parameters, as long as they are defined public or serializable. We can use both simple variable or arrays.
public float speed; public Sprite[] images; private float internalBleed; //cannot be controller from Unity Editor interface [SerializeField] private Text scoreText; [SerializeField] private GameObject[] enemyTypes;
MonoBehaviour Event Execution Order
Ordered by first to last method to execute.
private void Awake() { /* Called when the script is being loaded */ } private void OnEnable() { /* Called every time the object is enabled */ } private void Start() { /* Called on the frame when the script is enabled */ } private void Update() { /* Called once per frame */ } private void LateUpdate() { /* Called every frame after Update */ } private void OnBecameVisible() { /* Called when the renderer is visible by any Camera */ } private void OnBecameInvisible() { /* Called when the renderer is no longer visible by any Camera */ } private void OnDrawGizmos() { /* Allows you to draw Gizmos in the Scene View */ } private void OnGUI() { /* Called multiple times per frame in response to GUI events */ } private void OnApplicationPause() { /* Called at the end of a frame when a pause is detected */ } private void OnDisable() { /* Called every time the object is disabled */ } private void OnDestroy() { /* Only called on previously active GameObjects that have been destroyed */ }
Physics updates on a Fixed Timestep are defined under Edit ▸ Project Settings ▸ Time ▸ Fixed Timestep and may execute more or less than once per actual frame.
private void FixedUpdate() { /* Called every Fixed Timestep */ }
Debug
Debug.Log(transform.position); Debug.Log("text");
GameObject Manipulation
/* Destroy a GameObject */ Destroy(gameObject); /* Finding GameObjects */ GameObject myObj = GameObject.Find("NAME IN HIERARCHY"); GameObject myObj = GameObject.FindWithTag("TAG"); childObject=parentObject.GetChild("child_name"); parentObject.GetChild("child_name").GetComponent<SpriteRenderer>().sprite = image; /* Accessing Components */ Example myComponent = GetComponent<Example>(); AudioSource audioSource = GetComponent<AudioSource>(); Rigidbody rgbd = GetComponent<Rigidbody>(); GetComponent<SpriteRenderer>().sprite = image; //set image in child component GetComponent<Text>().text = '123' //set text /* Activate - can hide or how an element from the scene*/ myObject.SetActive(false); // hide myObject.SetActive(true); // show GetComponent<BoxCollider>().enabled = false; // hide component