Basic C# questions

Hi,

I am still a bit confused on how to use the ‘void’ function in C#. I know ‘void’ means ‘nothing’ or ‘no type’ and returns no value, but why do I have to write it for example in the following contexts:

  • void Start()
  • void Update ()

Why can’t I just write Start() or Update () ?

I somewhat understand the meaning of Start() now, but why do I need to write Update() ?

Also, how do I know when to write void Update() VS. void FixedUpdate() ?

Many thanks :slight_smile:

Hello Alex, how are you?

So, both Start and Update methods don’t return anything, it just runs the block of code inside it. For example, you can make a method that returns a int type like:

int subtract(int a, int b)
{
 Return (a-b);
}

and then use it as an regular int just like:

if (subtract(myint, 5) >= 0)
{DoSomething();}

This is an example of a method that return an int, both Start() and Update() don’t return any type, they are just methods that the engine call in some event situations, that is why they are void.

Once the script is initialized in the runtime, it will run the Start() method one time, and for every following frame it will run the Update() method once, that is why the Update() isnt called in a fixed amount of time, it will vary depending on how long it took to calculate the frame. FixedUpdate() is a event method that is called a fixed amount of times each second (it doesn’t depend on the frames per second), you can change it under time in the project settings (default value is 1/50).
You don’t have to call any of those methods in your scripts, only if you need something running in those respective events.

FixedUpdate() is better for handling physic calculations (the engine runs things like velocity, animations etc in the FixedUpdate()), and everything else can be added to the regular Update() since this is updated every frame

1 Like

Many thanks :))
it’s much more clear now :slight_smile:

happy new year

1 Like

Happy new year for you too!!
I am glad that I was able to help, let us know if you need something else.

Because if you don’t specify a return type (void, bool, int, whatever), the compiler considers it a constructor, not a method.

I.E.:

is considered a method, with return type T.

is considered a constructor.

[Access modifier] = public, private, protected, etc.

Privacy & Terms