Videogames Development – Unity – How to add a script
ADD SCRIPT
1. MAIN TOP MENU> Assets> Create Javascript
2. Assets> select NewBehaviourScript Import Setting
3. Inspector> ‘Open’ button, it starts MonoDevelop-Unity editor
4. Write:
function Start() { print( "Hello, world" ); }
5. MAIN TOP MENU> GameObject> Create Empty
6. Hierarchy> select ‘GameObject’ you have just created> Inspector> ‘Add Component’> Scripts> NewBehaviourScript
7. Play button> BOTTOM LEFT you will see ‘Hello, world’
BASIC STRUCTURE
#pragma strict function Start() { ... it runs one time when game start ... } function Update() { ... it runs every frame ... }
NOTICE:
#pragma is a pre-compiler directive. ie it tells the compiler what to do or how to behave.
With #pragma you must tell of what type the variable will be and it will give you an error if you don’t comply.
ADVANCED STRUCTURE WITH FUNCTIONES
#pragma strict var... declare initial value of variables -> DEFAULT ACCESS IS PUBLIC // Single line comment /* Multiline comment */ function Start() { ... it runs one time when game start ... } Myfunction() { ... the other funcions ... } function Update() { ... it runs every frame ... Myfunction(); // call external functions }
AWAKE – START – UPDATE
#pragma strict function Awake () { ... setup ammo for the enemy ... it starts at first, even component is not enable } function Start () { ... allow enemy to shoot ... it starts when component is enabled } function Update() { ... hits control ... it is called once per frame to control data changes over time, use this to: - moving non physics objects - simple timers - receiving input NOTICE: update interval times vary RESULT SAMPLE OVER TIME: 0.01656623 - 0.01656666 - 0.01656696 (it changes) } function FixedUpdate() { ... hits control ... it is called at fixed update intervals: - called every physics step - fixed update intervals are consistent - regular updates: adjusting physics objects NOTICE: update interval times is constant RESULT SAMPLE OVER TIME: 0.02 - 0.02 - 0.02 (it does not change) }