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?

Have a look at the doc :

  • HERE for the Update method
  • HERE for the Start method

If this is not enough, you can find some links like this one to complete the doc.

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

Update

Almost 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

When 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.