Difference between revisions of "Basic Scripting"

From __space Wiki
Jump to: navigation, search
(Created page with "With Unity you can use javascirpt, c#, or boo, and while the 3 are pretty similar, there are some major differences. I'm only going to talk about c# here as that is what most...")
 
Line 1: Line 1:
With Unity you can use javascirpt, c#, or boo, and while the 3 are pretty 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. [https://www.tutorialspoint.com/csharp/ 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.
+
*Work in progress 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. [https://www.tutorialspoint.com/csharp/ 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=
 
=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=
 
=Vector and transform manipulation=
 +
 +
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);
 +
 +
otherwise you need to declare the cube as a game object before Start()
 +
  
 
=Timing=
 
=Timing=
Line 14: Line 35:
  
  
=Getting components and their attributes from gameObjects=
+
=Getting components and their attributes from Game Objects=
 
(use a light as an example)
 
(use a light as an example)
  

Revision as of 15:54, 22 May 2017

  • Work in progress 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

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);

otherwise you need to declare the cube as a game object before Start()


Timing

Start

Update

Awake

FixedUpdate

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