Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrDude · Dec 11, 2018 at 08:39 AM · scripting problemgenerics

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Dec 11, 2018 at 09:41 AM 0
Share

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);
         }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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... :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges