Interactivity - Scripting Basics

The first thing anyone needs to know about scripting is how to create a new script, to do this is very simple and all you need to do is right click in the project window and click Create - C# Script. It is useful as well to make a new folder within the assets folder for the scripts, this helps keeping organised and knowing where everything is. Once you've created a new C# script and sensibly named it, always starting with a capital letter for every word, then double click it and MonoDevelop will open which is the default C# script editor for Unity and comes with the program.
There were a few keywords we were introduced to in the beginning of Unity which were:
  • Variables – These are the containers for the information you want to store in your scripts.
  • Data Types – These are the different types of variables that you can store different types of information.
  • Functions – A block of code comprised of many statements that is used to achieve a specific task.
We were also introduced to IF statements:

  • IF statements are used in scripting to test if a statement is true or false. If the conditions meet our requirements then a specified task will be executed. For example: If <enemy health is less that 1> then <destroy enemy>
  • IF ELSE / ELSE statements are used in situations where you also want something to happen when your conditions are not met.
  • It is possible to test multiple statements using the “AND” and “OR” operators. There is also a “NOT” operator for testing when something is not true.



As you can see on the example shown above theres something called a public variable, there are three types of variables like this within scripting and they are public, private, and protected but we only really use public and private. These control who sees the scripting within the section:

  • Public variables, are variables that are visible to all classes.
  • Private variables, are variables that are visible only to the class to which they belong.


We also have something called 'void Start' and 'void Update', these mean two different things to anything scripted within them. Anything within 'void Start' only happens during the beginning of the game, and anything within 'void Update' is called once per frame so is repeated throughout unless told otherwise.

Unknown

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment