unity dictonary code example

Example 1: onmouseclick unity

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;public class ExampleClass : MonoBehaviour
{
    void OnMouseDown()
    {
        // Destroy the gameObject after clicking on it
        Destroy(gameObject);
    }
}

Example 2: unity pingpong

void Update()
    {
        // Set the x position to loop between 0 and 3
        transform.position = new Vector3(Mathf.PingPong(Time.time, 3), transform.position.y, transform.position.z);
    }

Example 3: Dictionaries in unity

Dictionary<keyClass,valueClass> dictionaryName;
dictionaryName = new Dictionary<keyClass,valueClass>();

Example 4: unity dictionary

//create dictionary "test", with key as float and value as GameObject
private Dictionary<float, GameObject> test = new Dictionary<float, GameObject>();

//add thing to dictionary
private void addToDictionary()
{
  //add key 10 and this gameObject
  test.Add(10, transform.gameObject);
}

private void printFromDictionary()
{
  //try to get value and save it as out GameObject result
  test.TryGetValue(10, out GameObject result);
  //then print that result
  print(result);
}

private void deleteFromDictionary()
{
  //remove key and gameObject 10 from our Dictionary
  test.Remove(10);
}

Tags:

Misc Example