Understanding First Unity C# Script
Notes:
Understanding First Unity C# Script:
using UnityEngine;
public class FirstScript : MonoBehaviour
{
void Start()
{
Debug.Log ("Hello Unity");
}
void Update()
{
Debug.Log ("Updating");
}
}
public:
- is an access modifier; which makes the class accessible publicly (i.e. to unity engine)
class:
- keyword is used to create a class
- which allows us to manage properties and behaviors of a game object efficiently
Class Name:
- must be same as the file name
- public class name must be same as file name
- there should be only one public class per script
{ & } :
- indicate beginning and ending of a construct
Colon symbol:
- indicates extends in C#
MonoBehaviour:
- As start and update methods are part of MonoBehaviour class,
- we must extend our class with MonoBehaviour
- and
- convert our class into a component; so that we can attach it to some game object
using UnityEngine; :
- As MonoBehaviour & Debug class are present inside UnityEngine namespace
- we must use Unity Engine namespace.
void Start() { }:
- is called only once; just before first Update() call
- is used to setup initial state of a game object
void Update(){ }:
- is called continuously; once per frame based on system frame refresh rate
- is used to modify state of a game object every frame
Debug.Log(message); :
- Logs a given message to Unity Console window
Note:
As we are not referring anything from System.Collections & System.Collections.Generic
namespaces we can delete them.
using System.Collections;
using System.Collections.Generic;