Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- public class ObjectsStorage : MonoBehaviour
- {
- public GameObject[] items;
- private Dictionary<string, GameObject> itemsByName;
- public GameObject GetObjectByName(string name)
- {
- if (itemsByName == null)
- FillItemsByName();
- GameObject foundedObject;
- if (!itemsByName.TryGetValue(name, out foundedObject))
- Debug.LogError("Not found [" + name + "] in objects storage.");
- return foundedObject;
- }
- private void FillItemsByName()
- {
- itemsByName = new Dictionary<string, GameObject>();
- foreach (var item in items)
- {
- itemsByName[item.name] = item;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement