- Home /
Help with Functions
Hi, I am new to using unity but i have a good grasp on it.
The one thing that slips me up in javascript is the simplest thing it seems, I don't understand the Function Update and Function Start and how / when to use them. Could someone please explain this to me?
Answer by KiraSensei · Apr 08, 2014 at 04:19 PM
Have a look at the doc :
If this is not enough, you can find some links like this one to complete the doc.
Answer by Stormy102 · Apr 08, 2014 at 04:48 PM
function Update() {
}
This is called every frame. Basically, your screen is flickering so fast that the human eye can't see it. A typical computer does sixty frames per second. Anything in function Update() is executed every frame.
function Start(){
}
When your game is started, the code in this is executed first. So, if you want to instantiate a Prefab when the game is started, you would put it in function Start()
Hope this helps!
Stormy102
Not really : the Start function is called when the script containing it is instantiated, so not necessarily at game start.
Answer by ypsilon · Apr 08, 2014 at 04:47 PM
Update
lmost every game that you'll ever play consists basically on a big chunk of code that executes inside a loop. This loop goes on forever, and in each iteration of the loop, the state of the game is updated and changed according to different stimuli (user input, previous state of the game, etc). Well, the Update function in Unity is just that: a place to put all the instructions that you want Unity to execute on each iteration of the loop. To put it simple, what Unity will do on each iteration of this big loop is to take every object in your game and call its Update function. So, inside the Update function of each of your objects you will write the logic necessary to update their state.
Start
hen you start your game (hit the "play" button), all these objects must be first initialised; this is, their properties and attributes must be set up. This has to be done before we start the loop. Well, the Start function is the right place to do this. Unity will call the Start function of each one of your objects to initialise the properties. So inside of this function you will write the code necessary to set whatever attributes of that object you want to set.
I hope this helps you a little bit.
I'm working on units being damaged
I start them off all with an assigned var of 100, if i want the game to keep checking their hp i'd put thet script in the update function?