Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 elrohirgt · Dec 06, 2020 at 06:21 PM · 2d gamenullreferenceexceptiongetcomponentgenerics

GetComponent returns null when using a generics class for T

So I'm making a game where I have a Player.cs script that instantiates the player ship, the player ship prefab has a StandardShip component attached to it. StandardShip prefab view from the editor The StandardShip component inherits from PlayerShip which inherits from ship and so on until eventually it inherits from MonoBehaviour.

StandardShip.cs

 public class StandardShip : PlayerShip<LaserBullet> {...}

PlayerShip.cs

 public abstract class PlayerShip<T> : Ship where T : Bullet {...}

LaserBullet obviously inherits from Bullet, which in turn inherits from Monobehaviour too. Bullet.cs

 public abstract class Bullet : MonoBehaviour, IPoolableEntity {...}

The error occurs inside the Player.cs file, GetComponent returns null even thought all the components inherit from MonoBehaviour so I don't know what I'm doing wrong.

Player.cs

 string pathToPrefab = $"Prefabs/{PlayerPrefs.GetString(PlayerPrefsConstants.SELECTED_SHIP, null) ?? "StandardShip"}";
         GameObject objectToInstantiate = Resources.Load<GameObject>(pathToPrefab);
         PlayerShip<Bullet> ship = objectToInstantiate.GetComponent<PlayerShip<Bullet>>();
         //var s = objectToInstantiate.GetComponent<Rigidbody2D>();

The Resources.Load returns the correct prefab and I tried getting the RigidBody2D, it worked. So I don't know what the problem is. I would really appreciate if someone could tell me what I'm doing wrong 'cause I can't figure this out.

captura-de-pantalla-2020-12-06-a-las-114913.png (41.7 kB)
Comment
Add comment
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

1 Reply

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

Answer by sacredgeometry · Dec 06, 2020 at 06:41 PM

Probably for the same reasons:

 // These work      
 var a = GetComponent<StandardShip>();
 var b = GetComponent<PlayerShip<LaserBullet>>() as StandardShip;
 
 // These do not        
 var c = GetComponent<PlayerShip<Bullet>>() as StandardShip;
 var d = (StandardShip)GetComponent<PlayerShip<Bullet>>();

i.e. When using GetComponent StandardShip is not a PlayerShip<Bullet> it's a PlayerShip<LaserBullet>because it doesn't support variance in that direction.

Reading

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/ https://docs.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance

Comment
Add comment · Show 1 · 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 elrohirgt · Dec 06, 2020 at 07:18 PM 0
Share

Thank you for the answer, I didn't know this covariance and contravariance. I didn't know Generic with a derived class can't be casted to the same generic but with a base class so I'll have to search for another way of doing this.

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

190 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 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

NullReferenceException. GetComponent dose not work properly 2 Answers

Why is this null? Finding a script on an object 3 Answers

Object reference not set to an instance of an object in c#. 0 Answers

Assign collider and linerenderer to list 1 Answer

NullReferenceException - Editor script using GetComponent after AddComponent 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