Basic Scripting

From __space Wiki
Revision as of 14:53, 25 May 2017 by Steph (talk | contribs) (Timing)
Jump to: navigation, search
*Work in progress page!*

Main guide page

With Unity you can use javascript, c#, or boo, and while the 3 are somewhat similar, there are some major differences. I'm only going to talk about c# here as that is what most Unity users use and the language you will find the most helpful online answers for. If you don’t already know C#, please go learn it first. A good c# tutorial. This scripting guide can be skipped to come back to later, but it is highly suggested that you go through this material at some point.

Another small note: my editor for scripts looks different from the default of Monodevelop but it works the same way and the code is the same as it would be if I was using MonoDevelop. If you want to use the same one as me it is called Sublime Text, but this is entirely a personal preference.

To create a new script go to your project tab and right click anywhere then Create > C# script. Name it whatever you want, as long as you can remember it. You can also add a script onto an object by using 'Add Component' and either finding or typing in 'New Script'. Scripts actually can only ever affect things in the game if they are attached to an object. Any script you make can be found by searching when using 'Add Component' or dragged onto the object in Hierarchy or when an object is in the inspector.

Say we want a specific cube to be half its size when we press the 'h' button in-game. In that case we first need to know which cube to resize, you can do that either by having the script be a component of the cube in question, in which case you don't need to do anything else to know which cube, or you can have the object be a variable you pick.

Detecting keyboard input

To check whether the H key has been pressed say

if(Input.GetKeyDown('H')){
   print("H key has been pressed!"); }

This will make a "H key has been pressed!" message appear in the Console tab upon the first frame an 'H' key is pressed (so it only does the thing between the curly braces once for every time the user presses the button).

Vector and transform manipulation

Continuing with the example from the previous section, to then resize that cube on that signal, you would change the code within the curly braces of the if statement to:

  • if the script will be going on the cube:
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

And you don't have to do much else besides make sure the script is on the proper game object, of course.


  • To change the cube if the script is a component of some other object that isn't the cube, your code would look more like this:

CubeSmallerscript.png

The declaration of cube and its placement before Start() is important so it has the right scope for it to a known variable in both Start() and Update().

The script can then be placed on any object in the scene, but you will have to tell it what cube is.

ApplyScript.gif

Timing

Start

Start() is often where you initialize variables, or where you can call a function if you only want it to happen once at the start of play.

Update

Update() is called once per frame, and is probably where the bulk of your code will be, or at least where it gets called.

Awake

Awake() is called right before Start() or after the object the script is on has been instantiated. If the object is deactivated at startup, Awake() is still called when, or if, the object is set to active.

Activation/ deactivation can be toggled in the inspector. ActiveCheckbox.png

It can also be toggled via script by saying {object}.SetActive(true); or {object}.SetActive(false);, and {object} must be of type GameObject.

FixedUpdate

Most physics-related updates should happen in here because physics changes are done directly after FixedUpdate(). FixedUpdate() is also different from Update() in that it happens at a fixed rate while Update() varies based on the framerate the game is currently running at.

For more information visit Order of execution

Getting components and their attributes from Game Objects

(use a light as an example)

Empty Game Objects

They're actually kind of useful! You can put scripts on them and make it a kind of "manager" of the rest of the objects in the scene. So all the scripts can be in one place if you want and that might make things easier.

Colliders