Difference between revisions of "Basic Scripting"
m (→Vector and transform manipulation) |
m (→Vector and transform manipulation) |
||
Line 24: | Line 24: | ||
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); | transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); | ||
− | + | And you don't have to do much else besides make sure its 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: | ||
+ | [[File: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. | ||
=Timing= | =Timing= |
Revision as of 13:59, 25 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.
Contents
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 its 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:
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.
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.