= Variables y entorno =======

In C#, you must declare a variable as public to see it in the Inspector. If not:
[HideInInspector]
public float strength;

Note: the ThreadStatic attribute defined in the .NET library should not be used as it will cause a crash if added to a Unity script.

The simplest and most common case is where a script needs access to other Components attached to the same GameObject. This is done with the GetComponent function. Typically, you want to assign the Component object to a variable, which is done in C# using the following syntax:

void Start () {
    Rigidbody rb = GetComponent<Rigidbody>();
}

Others:

void Start () {
    Rigidbody rb = GetComponent<Rigidbody>();   
    // Add a force to the Rigidbody.
    rb.AddForce(Vector3.up * 10f);
}

transform.position = player.transform.position - Vector3.forward * 10f;

Finding Objects by Name or Tag

It is always possible to locate GameObjects anywhere in the scene hierarchy as long as you have some information to identify them. Individual objects can be retrieved by name using the GameObject.Find function:

GameObject player;
void Start() {
    player = GameObject.Find("MainHeroCharacter");
}

GameObject player;
GameObject[] enemies;
void Start() {
    player = GameObject.FindWithTag("Player");
    enemies = GameObject.FindGameObjectsWithTag("Enemy");
}

Important classes:
MonoBehaviour	
The base class for all new Unity scripts, the MonoBehaviour reference provides you with a list of all the functions and events that are available to standard scripts attached to Game Objects. Start here if you’re looking for any kind of interaction or control over individual objects in your game.

Transform	
Every Game Object has a position, rotation and scale in space (whether 3D or 2D), and this is represented by the Transform component. As well as providing this information, the transform component has many helpful functions which can be used to move, scale, rotate, reparent and manipulate objects, as well as converting coordinates from one space to another.

Rigidbody / Rigidbody2D	
For most gameplay elements, the physics engine provides the easiest set of tools for moving objects around, detecting triggers and collisions, and applying forces. The Rigidbody class (or its 2D equivalent, Rigidbody2D) provides all the properties and functions you’ll need to play with velocity, mass, drag, force, torque, collision and more.

FixedUpdate function is called just before each physics update.

The OnCollisionEnter, OnCollisionStay and OnCollisionExit functions will be called as contact is made, held and broken. The corresponding OnTriggerEnter, OnTriggerStay and OnTriggerExit functions will be called when the object’s collider is configured as a Trigger (ie, a collider that simply detects when something enters it rather than reacting physically).

To change constantly the position:
transform.Translate(0, 0, distancePerSecond * Time.deltaTime);

a GameObject can be created using the Instantiate function which makes a new copy of an existing object:
public GameObject enemy;
void Start() {
    for (int i = 0; i < 5; i++) {
        Instantiate(enemy);
    }
}

void OnCollisionEnter(Collision otherObj) {
    if (otherObj.gameObject.tag == "Missile") {
        Destroy(gameObject,.5f);
    }
}

Destroy(this);

Other classes:
Application.CaptureScreenshot(name);

= Corroutines ===============

IEnumerator Fade() {
    for (float f = 1f; f >= 0; f -= 0.1f) {
        Color c = renderer.material.color;
        c.a = f;
        renderer.material.color = c;
        yield return null;
    }
}

void Update() {
    if (Input.GetKeyDown("f")) {
        StartCoroutine("Fade");
    }
}

= Global Objects ============

public class GameSystem : MonoBehavior
{
  private GameSystem m_Instance;
  public GameSystem Instance { get { return m_Instance; } }

  void Awake()
  {
    m_Instance = this;
  }

  void OnDestroy()
  {
    m_Instance = null;
  }

  void Update()
  {
    // global game update logic goes here
  }

  void OnGui()
  {
    // common GUI code goes here
  }

  // etc.
}
You can then create an object called "GameSystem" in the root of your scene. The only components it would have would be the built-in transform component (set its position to the origin, its rotation to identity, and its scale to one; not that it matters, but it's good practice). Attached the GameSystem component to that object.

You can now access your global object by simply using GameSystem.Instance.blah(). Its event handler methods are invoked automatically by Unity since it derives from MonoBehavior and exists as a component. You can add fields to it that reference other game objects or components and connect them in the Unity object hierarchy view.

= Debug =====================

Debug.Log("I am alive!");
Debug.Break(), stops the game.
print

= Basics ====================

function Update() {
	if (Input.GetKeyDown(KeyCode.Z)) {
		renderer.enabled = true;

Esto pasa a ser:
		GetComponent<Renderer>().enabled = !GetComponent<Renderer>().enabled;

Lifetime functions:
Awake(), it's called while the script is loaded. Used to initialize instances. At this point it can not interact with other GameObjects.
Start(), it's called after Awake(). The difference is that this method won't be called if the script is not enabled.
Update(), update funcition, one by frame.
FixedUpdate(), update function to be used for physic operations. It will be called so many times as needed before drawing a frame.
Destroy(), called when the object is destroyed.
LateUpdate()
OnDisable()
OnGUI()



DontDestroyOnLoad(object), the object can be this, :!:
Instantiate(GameObject)
Invoke(name, time)
GameObject.Find(name)
gameObject.GetComponent<typeOfComponent>() gets the first component (script, whatever…) from a GameObject object. There is also GetComponents<type>() that retrieves a list. The GameObject must be active.
GetComponentInChildren<Type>(), retrieves from children object.
Destroy(gameObject, time), will destroy an instatiated prefab on 20secs.
Destroy(gameObject), destroys the object now.
SendMessage, calls a method on each script which have it inside the GameObject.
// Calls the function ApplyDamage with a value of 5
gameObject.SendMessage ("ApplyDamage", 5.0);
...	
// Every script attached to the game object 
// that has an ApplyDamage function will be called.
function ApplyDamage (damage : float)



http://docs.unity3d.com/ScriptReference/Animation.html
http://docs.unity3d.com/Manual/AnimationOverview.html
https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting?playlist=17120
https://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter?playlist=17120

Camera follow the mouse:
transform.LookAt(camera.ScreenToWorldPoint (Vector3 (Input.mousePosition.x,Input.mousePosition.y,camera.nearClipPlane),Vector3.up);

Billboard:
public class Billboard : MonoBehaviour
{
    void Update() 
    {
        transform.LookAt(Camera.main.transform.position, -Vector3.up);
    }
}

Change color:
var cursor = GameObject.Find("Plane");
var renderer = cursor.GetComponent<Renderer>();
renderer.material.color = color;

Access to parent:
MainScript.instance.setVisible(this.gameObject.transform.parent.gameObject, false);

= Animation =================

Know if an animation is playing:
print (animation.isPlaying);

Play animation:
var player = GameObject.Find(gameObject);
    setVisible(player, true);
    var animation = player.GetComponent<Animation>();
    animation.Play(clip);

Make visible or invisible:
  public void setVisible(GameObject gameObject, bool visible) {
    var renderers = gameObject.GetComponentsInChildren<Renderer>();
    foreach (Renderer render in renderers)
      render.enabled = visible;
  }


  For Unity 5 ambient scene lighting is now found under Window > Lighting > Scene.

= Import a FBX ==============

First of all you need to recover animations

 Project > Root Element > Rig > Animation Type > Legacy > Apply

For textures you need to put shaders as "Legacy shaders/vertexlit" y seleccionar la textura

= To use Oculus in Mac =

YOu need to install the oculus sdk

= to use the cardboard =====

Unity: https://developers.google.com/cardboard/unity/

You need to deselect in Build settings > Android

deselect virtual reality supported and gpu skinning (if not will ask for the glasses). In resolution and presentation, the default orientation would be 'Landscape Left'


