Un Flashcards
A sprite is
An image you imported to your game.
Note:
It is a bitmap, which means pixels and colors.
The z-index of game objects is stored under
order in layer
Note: Higher numbers are more visible
The core functions of game object scripts are
Start: Initialization game objects (ie RigidBody)
Update: meant for input checks
FixedUpdate: meant for creating physics movements
To make a variable from the script available in the inspector, type
public int myInt = 1;
c# sheet
To write a float, type
0.1f
To write a function, type public int MyFunc(int number1) { Console.WriteLine("I'm doing something..."); }
Remember to make an explicit declaration when declaring a variable List-GameObject> allSoldiers = new List();
C# uses angle brackets for Generic Type parameters, meaning when you need to pass in an uninstantiated type class.
To create a class, type class ClassName {}
Void functions
Do not return a value
To write a method that doesnt return anything, type
static void MethodName()
{}
The first mandatory method called in any script is
static void Main()
{}
To set the namespace, type
namespace NameSpaceName
{}
To convert a number string into an int, type
int.Parse(“10”)
types can also have
methods. e.g. int.Parse(“10”)
To create a while loop, type
while (varName == true)
{}
To create an if else, type if (varName == "string") {} else if (varName == "string") {} else {}
To make a loop restart back from the beginning, type
continue
To write a try/catch, type try {} catch(ExceptionName) {}
Variables are only inside
the curly brackets they were declared in
To convert a string to lower case, type
stringVar.ToLower()
The types that can hold decimals are
double, float
To cast a double into an int, type
(int)(10.0)
Note: Casting a number does not round it, it truncates.
“using” statements automatically
call the dispose function on the object after it is used
To initialize a list, type List = new List()
The difference between a list and an array is
You dont need to set the size of a list, but an array you do.
To make an array: int[]
To make a list: List
To initialize an int array with values, type List = new List {1,2,3,4,5} int[] varName = new int[5] {1,2,3,4,5} or var varName = new int[5] {1,2,3,4,5} or List = new List {1,2,3,4,5}
To initialize a dictionary in c#, type Dictionary dictName = new Dictionary(); Note: c# does not provide and empty list as the value even if the type is set to list
To add to a dictionary, type
dictName.Add(“key”, “value”)
To get a dictionary value by the key, type
dictName[“key”]
The static modifier is used to Make a function a class function instead of an instance function
public methods can be called by
anyone
private methods can be called my
only other functions inside the same class
To avoid having to write the namespace, type
using NameSpaceName
note: No ;
Namespaces can be
Nested multiple times
To enter the c# repl, type
csharp
A common compiler for c# is
mono
You can compile c# scripts using mono by typing
mcs ScriptName.cs
To write to the console, type
Debug.Log(“string”);
System.Console.Write(“string”);
System.Console.WriteLine(“string”);
To get input into the console, type
System.Console.ReadLine();
Edits you make while in play mode
get deleted when you unplay
A tileset is
an image with small sprites that you can drag to repeat
A box collider 2d is
a box drawn around a game object to give it a barrier that creates collision events
Note: There are other shape colliders
To listen for presses on the arrow keys, type
Input.GetAxis(“horizontal”)
Note: The value will return 1 if they press right and -1 if they press left
Input.GetAxis(“vertical”)
Note: The value will return 1 if they press up and -1 if they press down
Note: To get these numbers in a more snappy way, type
Input.GetAxisRaw(“…”)
The two ways to make an object move are
Change the transform.position of the game object
or
Add a force to a RigidBody2D
You can get premade assets
by googling “unity assets 2d”
To get the current position of a game object in it’s script, type
transform.position
To trigger animations from a script, type
animator = GetComponent()
animator.play(“animation_name”)
This eliminates the need for the visual UI
A vector is
….
To add a script to a game object from within a script, type
public GameObject myObject; // Or get and existing one
myObject.AddComponent();
To pan the editor
To zoom in the editor
option + mouse drag
control + option + mouse drag
To detect a click, type
if (Input.GetMouseButtonDown(0)) {
Debug.Log(Input.mousePosition);
}
To load an asset into a script, you must
create a folder directly under the assets fold called “Resources”
Place an asset in there
Load the resource ie
Resources.Load(“AC Player”) as RuntimeAnimatorController
Note: Do not include the “Assets/” or the file extension
The difference between GetComponent and AddComponent is
GetComponent gets a component that is already added to the object the script is on.
Note: returns null if component is not already added.
AddComponent is used to add a new component to an object.
Note: Calling multiple times will attach multiple instances.
When you add a new component to a game object, it returns
an instance of that component so you can alter its properties after
Rigidbody2D rb = newGameObject.AddComponent();
rb.gravityScale = 0;
It is easier to access a game objects transform object by
using the dot notation than instantiating
newGameObject.transform.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y)
rather than
Transform newGameObjectTransform = newGameObject.transform
Note: Do game objects automatically get a transform component?
To convert a mouse position to the position in the game, type
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
MyGameObject.transform.position = new Vector2(worldPosition.x, worldPosition.y);
It seems that if you add an animator component to a game object, you do not need to add to the sprite renderer a
sprite
To make a field in the inspector component show up as a dropdown
use an Enum value as the public variable type
For the game to listen to keyboard events,
the mouse must be hovering over the game window
To get the direction of an object thats moving, type
Vector2 currentDirection = (currentPosition-previousPosition).normalized;