gh.amani
gh.amani

Reputation: 31

Initilizing a dictionary issues

Instead of just initilizing

Screens (Dictionary<GameObject, string> Screens) values

this code creates a new gameObject screens with these associated values.

The problem is here:

Screens[new GameObject(cont.Key)] = cont.Value;

This is the result for screens : "Screens are created again with the associated values RightOuterPad=, LeftOuterPad=, LeftScreen=, RightOuterPad=,OPENPAD1-video-2 ,LeftOuterPad=OPENPAD1-video-1, LeftScreen=MAINBOARD1-video-2

Dictionary<string, string> content = new Dictionary<string, string>();

content = ReadConfigFile(path);

Dictionary<GameObject, string> Screens = config.Screens;

List<string> ScreensNames = new List<string>();

foreach (var screen in Screens)
{
    ScreensNames.Add(screen.Key.name);
}

foreach (KeyValuePair<string, string> cont in content)
{
    if (ScreensNames.Contains(cont.Key))
    {
        Debug.Log(Screens);
        Screens[new GameObject(cont.Key)]= cont.Value;
    }
}

Upvotes: 0

Views: 88

Answers (1)

derHugo
derHugo

Reputation: 90724

It sounds like what you are trying to do is

using System.Linq;

...

var keyObjectReference = Screens.keys.FirstOrDefault(o => string.Equals(o.name, cont.Key));
config.Screens[keyObjectReference] = cont.Value;

this instead retrieves the first GameObject that has a matching name with cont.Key and uses that reference instead for accessing the correct entry in the config.Screens dictionary.

Upvotes: 2

Related Questions