Assigning a script as a game object in Dictionary

Hello everybody,

I have a script called “HouseProperties” and I want all the objects with this script to be in my dictionary of string and gameobjects Dictionary<string, gameObject> houseProp = new Dictionary<string, gameObject>();

Is it possible to write somthing like GameObject houseValue = HouseProperties.gameObject because this script is located on a gameobject?

I got an error of course, is there a way to write it correctly?

Thank you!

So, you want to collect all script instances, and store their game objects and names in a dictionary ?

You can use Linq for that, it makes it easy to read.

using System.Linq;

Dictionary<string, GameObject> housesDictionary;
HouseProperties [] allHouses = FindObjectsOfType<HouseProperties>();
housesDictionary = allHouses.ToDictionary(v => v.gameObject.name, v => v.gameObject);