Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.74 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class ObjectsStorage : MonoBehaviour
  5. {
  6.     public GameObject[] items;
  7.     private Dictionary<string, GameObject> itemsByName;
  8.  
  9.     public GameObject GetObjectByName(string name)
  10.     {
  11.         if (itemsByName == null)
  12.             FillItemsByName();
  13.  
  14.         GameObject foundedObject;
  15.         if (!itemsByName.TryGetValue(name, out foundedObject))
  16.             Debug.LogError("Not found [" + name + "] in objects storage.");
  17.  
  18.         return foundedObject;
  19.     }
  20.  
  21.     private void FillItemsByName()
  22.     {
  23.         itemsByName = new Dictionary<string, GameObject>();
  24.         foreach (var item in items)
  25.         {
  26.             itemsByName[item.name] = item;
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement