cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for a usergroup in the current users assigned groups always returns false

Deena_Hess1
Star Contributor
Star Contributor

I know this has to be something simple, but the following code always returns false. I'm literally looping through the current users groups and it tells me that they aren't in them.

foreach(UserGroup grp in app.CurrentUser.GetUserGroups())
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Verbose, app.CurrentUser.Name + " is in " + grp.Name + ": " + app.CurrentUser.GetUserGroups().Contains(grp).ToString());
}

1 ACCEPTED ANSWER

Justin_Carlson
Content Contributor
Content Contributor

I don't think most Unity API objects have equality operators defined so when you obtain the User Group object in two separate ways, from a .NET perspective they are not equal. The Contain method can take in an IEqualityComparer<T> as the second parameter, but I think that requires creating your own class to check for equality (at least I don't know any shortcut way to use it). So you could do that, or you could also use some other method that let's you pass in a predicate. In some cases they don't always return a boolean so you could check for the expected outcome. For example if Find() returns null, or Count  > 0. The Any() extension method may be most similar to what you are looking for.

Here are the code examples that are all similar in returning a boolean, where the first one is the Contain method that will not give you the expected results, but the others should:

app.CurrentUser.GetUserGroups().Contains(grp)app.CurrentUser.GetUserGroups().Find(ug => ug.ID == grp.ID) != nullapp.CurrentUser.GetUserGroups().Count(ug => ug.ID == grp.ID) > 0app.CurrentUser.GetUserGroups().Any(ug => ug.ID == grp.ID)

View answer in original post

4 REPLIES 4

Joseph_Malecki
Confirmed Champ
Confirmed Champ

Change 
app.CurrentUser.GetUserGroups().Contains(grp).ToString()
To
app.CurrentUser.GetUserGroups().Contains(grp.Name)

I did try hard-coding a group name in, but the function is looking for a UserGroup object

Justin_Carlson
Content Contributor
Content Contributor

I don't think most Unity API objects have equality operators defined so when you obtain the User Group object in two separate ways, from a .NET perspective they are not equal. The Contain method can take in an IEqualityComparer<T> as the second parameter, but I think that requires creating your own class to check for equality (at least I don't know any shortcut way to use it). So you could do that, or you could also use some other method that let's you pass in a predicate. In some cases they don't always return a boolean so you could check for the expected outcome. For example if Find() returns null, or Count  > 0. The Any() extension method may be most similar to what you are looking for.

Here are the code examples that are all similar in returning a boolean, where the first one is the Contain method that will not give you the expected results, but the others should:

app.CurrentUser.GetUserGroups().Contains(grp)app.CurrentUser.GetUserGroups().Find(ug => ug.ID == grp.ID) != nullapp.CurrentUser.GetUserGroups().Count(ug => ug.ID == grp.ID) > 0app.CurrentUser.GetUserGroups().Any(ug => ug.ID == grp.ID)

The find method did the trick. Thanks!