- Home /
Why can't I use a variable of the same type as a static generic variable when using foreach?
This error just boggles my mind. Perhaps I don't understand generics as well as I thought. Please look at this and tell me what I am doing wrong:
public class Foo<T> : MonoBehaviour where T : Bar
{
static public List<Foo<T>> elements;
}
public class MyClass : Foo<MyClass> {}
//script 2
void LoopOverStuff ()
{
foreach (MyClass t in Foo<MyClass>.elements) Debug.Log(t.name);
foreach (MyClass t in MyClass.elements) Debug.Log(t.name);
}
The first foreach gives me an error saying it cannot convert from MyClass to MyClass (or whatever base class I used for T). I don't really WANT to do it this way cause it feels hacky and kinda defeats the purpose of having that as the base class and creating the derived classes from it. I resorted to trying this due to the other foreach annoying me with the warnings I can't get rid of...
The second foreach gives me a warning telling me I am accessing a static variable via a derived class but it works as expected without any errors. Just that annoying yellow squiggle in my editor that I can't seem to get rid of. If it doesn't want me to access the value via a derived class but it doesn't allow me to fetch the value from the base class directly then what am I supposed to do to please editor and the compiler? NOT making the field static would also go against the entire purpose of making the class so that is also not an option.
So why is it telling me I can't convert the generic value to a variable of the exact same type and why does it not have that issue when I go through the derived class?
I am using .NET 4 and have been having this issue since forever so not sure if it's Unity related as much as it is C# related. Anyone have any ideas?
Thanks
What do your $$anonymous$$yClass and Bar look like?
Note that this gives no errors...
class Bar
{
public string name;
}
class $$anonymous$$yClass : Bar { }
class Foo<T> : $$anonymous$$onoBehaviour where T: Bar
{
static public List<T> elements;
}
void LoopOverStuff()
{
foreach ($$anonymous$$yClass t in Foo<$$anonymous$$yClass>.elements) Debug.Log(t.name);
}
Answer by MrDude · Dec 11, 2018 at 10:38 AM
Whenever I finally get to the point where I ask for help I always feel like a complete n00b when the answer slaps me right in the face! Facepalm!!!
I am storing Foo in the list so I can't convert between Foo and T. MY problem was that the amount of namespaces and the nested types etc was making it hard for me to see that THAT was the error being thrown at me... Can't convert Foo to T... I misread that as "can't convert T to T". i just tried this and it worked perfectly...
foreach(Foo<MyClass> x in Foo<MyClass>.elements)
Uggg.... what an embarrassingly n00b mistake to make... :(
Answer by Casiell · Dec 11, 2018 at 09:21 AM
Ok, your example doesn't really work when copy-pasted and I don't have much time to fix this right now, but here is a quick something to try before you get more competent answet.
foreach (MyClass t in Foo.elements.Select(x => x as MyClass))
Select is part of System.Linq namespace
Your answer
![](https://koobas.hobune.stream/wayback/20220612183730im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why my Serializable class isn't receiving values from a Generic .json loader? 2 Answers
Pass Class as a generic type using string 0 Answers
Collider2D Resize Based on Sprite Using Generics 1 Answer
How to remove an item from a list of custom variables 1 Answer
,How to get the script of an element? or the GameObject of an Component? 1 Answer