You are on page 1of 6

[SerializeField] //attributo

transform.position

if(Input.getKeyDown(KeyCode.Space))
{
Debug.Log("space pressed");
}

Time.TimeScale

GameObject fracturedCrateObj = Instantiate(gameObject, transform.position,


Quaternion.identity) as GameObject;
RigidBody[] allRigidBodies =
fracturedCrateObj.GetComponentsInChildren<RigidBody>();
if(RigidBody-Length > 0){
foreach(var body in allRigidBoidies)
{body.AddExplosionForce(500, transform.position, 1)
Destroy(this.gameObject);

Debug.DrawRay(transform.position, directionToFace, Color.green);

Quaternion.Slerp(..);

switch(condition){
caso0:… break;
case1: …. break;
default:… break; }

coroutines (yield retuurn…)

public string[] names = new string[3];


public string[] names;
public string[] names = new string[] {"jhfhj", "dfhf"};

foreach (var item in items)

C# constructors-:
public class WeaponStats()
{
public string name;
public float attackRate;
//The following is the constructor:
public WeaponStats(string name, float attackRate)
{
//initialize things there
this.name = name;
this.attackRate = attackRate;
}
}
//define the objects in the "player" class that inherits monobehavior:
private WeaponStats blasters;
//then inizialize the objects in Start():
void Start()
{
blasters = new WeaponStats("Blaster", 0.25f);
}

var in the universal data type

serializing allows unity to read custom classes through the inspector:


above the custom class declaration we add an attribute:
[System.Serializable]

protected data members: (other than public and private references):


only the objects that are inherited can access the information and modify it

virtual methods and overriding:


if I use the same method in an inherited class, I have to override the method by
using a virtual method in the parent class and an override method in the inherited
class:
ex: protected virtual void Speak(){}
protected override void Speak(){}
When you call a virtual method, all override methods are controlled before executing
any of them.

I have to use monobehavior objects when I have to attach them game logic
use custom class to define blueprint for your programs

structs, memory management and value vs reference types:


[structs are referred as performance enhancer and replacements to classes]NO
structs are used when you have fewer than 4 fields and you don’t need to use
inheritance, they are similar to class
ex: public struct Item
{
public string name;
public int itemID;
//I can also have a constructor
public item(string name, int itemID)
{this.name=name;
this.itemID = itemID;
}
}
the difference between classes and structs is that classes support inheritance,
structs can’t be inherited (they are immutable, its data don’t change)
the difference is in memory management, a struct is considered a value type (stored
in the stack), a class a reference type(stored in heap)
[data types are considered value typed if they contain their data type, they are
primitives(char, float, int,…) and struct; they store their data directly]
[reference types does not store their data directly, it stores the address of where the
data is stored. Reference types contains a pointer to another memory location that
holds that data. Examples are classes, strings, arrays(does not matter what they
contain), delegates]
If I pass a value type to a method, it will clone (or copy) the value, but I cannot
change the original value, whereas
if I pass a reference type to a function, I mantain a reference to that object and I can
modify it.
When I create a new object Item (defined as a struct), I don’t need to use the "new"
operator, I don’t have to create an instance of an object, because it’s a value type.
reference types allow you to modify data in a function that takes it as a parameter, a
value type no (it copies the data and modifies it in the function, it does not touch the
outer value)..

use static type when you have data that is shared across the entire game and there
is only one of them
I can access a static data at class level:
Score.score += 10;
the data remains in the memory for the life of the program
when something is static I can’t see it in the inspector
static data (defined in the custom class) is shared across all instances of an object,
and can be accessed at class level (use Item.staticData, not sword.static.Data)

[Monobehavior methods: onEnable and OnDisable]

utility classes are static classes (cannot inherit monobehavior) filled with static
members

static constructor are called only once before the instance constructor.

properties (getters and setters in java) allow you to control access modifiers:
private bool isGameOver;
public bool IsGameOver
{
get
{
return isGameOver;
}
set
{
isGameOver = value
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)
{
IsGameOver = true; //uses the setter of the IsGameOver property
}}

an auto property allows to have getters and setters in a cleaner way.


public bool IsGameOver {get; set;}
the downside to auto properties is that I can’t run any function code within it.
I can add access modifiers to the get/set methods:
public bool IsGameOver {get; private set;}
I can’t see the property methods in the inspector (I can see them only in the debug
inspector)
properties must be initialized in the Start() method, can’t initialize them in the
declaration.
[jonathan uses properties in the manager scripts]

namespaces allow us to organize and mantain our code libraries, to prevent conflict
of names:
namespace WeaponPack
{
public class Weapon : monobehavior
{
}
}
I can access the Weapon class I created in another script by declaring in the first
lines ( before the class declaration):
using WeaponPack;

a list is very similar to arrays, but they can be extended at runtime:


public List<GameObject> enemiesToSpawn = new List<GameObject>();
public GameObject[] objectsToSpawn = new GameObject[10];
{
objectsToSpawn[0] = new GameObject();
enemiesToSpawn.Add(new GameObject);
}
we access the data the exact same:
objectsToSpawn[2].name = "john";
enemiesToSpawn[2].name = "ciao";
remove data:
objectsToSpawn[2] = null;
enemiesToSpawn[2].remove;

enums allow you to create readable selections that are based on integer values and
you can use them in an english format when you’re writing code.
public enum LevelSelector
{
Easy, //is assigned to 0
Normal, //assigned to 1
Hard, //2
//I can also assign the number manually:
Expert = 43,
SuperDuper = 324
}
to make use of it I create a variable of type LevelSelector:
public LevelSelector currentLevel;
now I can use the switch statement:
{
switch (currentLevel)
{
case LevelSelector.Easy:
break;
}}
[enums for custom class]
casting enums to int:
The data type I want to cast it to in parenthesis before the variable name:
SceneManager.LoadScene((int)LevelSelector);

with dictionary i can associate objects with values with a key/value pair:
public List<Item> itemList = new List<Item>();
public Dictionary<int, Item> itemDictionary = new Dictionary<int, Item>();

itemDictionary.Add(0, new Item());
var item = itemDictionary[0];
looping in dictionary:
foreach (KeyValuePair<int, Item> item in itemDictionary){}
(var key in itemDictionary.Keys){}
(Item item in itemDictionary.Value){}
the keys must be unique and must exist, you should always check before access an
object in a dictionary through key, if that key exist:
if ( itemDictionary.ContainsKey(70){}
use dictionaries when you. have large lists

abstract classes allow us to force inheritance (I have to implement(ovveride) the


class(method) in the inherited classes) and create easily manageable and
mantainable code
abstract classes cannot be instantiated (attached to a game object)
public abstract Enemy : Monobehavior
{
public abstract void Attack(); //abstract method does not support an implementation (I
can initialize abstract methods only in abstract classes)

interfaces are similar to abstract classes in the sense that they can force
implementations, anything that is in the interface must be implemented
interfaces cannot have the public or private fields (anything is assumed public) and I
can only use properties and methods
interfaces cannot inherit anything
public interface IDamageable
{
int Health {get; set;}
void Damage(int damageAmount);
}
public class Enemy : Monobehavior, IDamageable
{
public int Health {get; set;}
public void Damage(int damageAmount){}}
one benefit of using interfaces is that they allow multiple inheritance (classes can
inherit from only one class), I can implement as many interfaces as I want

polymorphism:
public interface IDamageable
{
int Health {get; set;}
void Damage(int damageAmount);
void Damage(float damageAmount);
}
Otherwise, I can also use polymorphism this way:
public interface IDamageable<T> //The T keyword signify that it is a generic type
{
int Health {get; set;}
void Damage(T damageAmount);
}
//so whatever I specify when I implement the interface, will be the type of T:
public class Enemy : Monobehavior, IDamageable<int>
{
public int Health {get; set;}
public void Damage(int damageAmount){}}
—>interfaces are very useful when I control if any of my objects implement the
interface:
RaycastHit hitInfo;
IDamageable obj = hitInfo.collider.GetComponent<IDamageable>();
if (obj != null) //it means that it is implementing the interface

you can think of delegates as a variable that holds a method or several methods:
public delegate void ChangeColor(Color = new Color);
{

You might also like