Introduction to C# scripting & collision detection for 2D games in Unity

This video gives an introduction to writing code for 2D games in Unity using the C# programming language. We’ll get started by adding collision detection so that our player object (or character) can collect points when colliding with coins in the game. You’ll learn how to setup 2D collision detection, increase a score variable, display messages in the console to test your code, and how to destroy objects (or make objects disappear) in a scene using code.

Watch the video below and scroll down to see the sample code.

Sample code

using UnityEngine;
using System.Collections;

public class CoinScript : MonoBehaviour {

  public int score = 0;

  // Use this for initialization
  void Start () {
  // Update is called once per frame
  void Update () {

  void OnTriggerEnter2D(Collider2D other){
    Destroy (gameObject);
    Debug.Log ("Score: " + score);

Next tutorial: Moving the player with C# code