Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by VanillaJudge · Jul 17, 2020 at 03:19 PM · variablesinheritance

One variable handling two types without a mess

Hello!


I am trying to build up a new scene and reuse the scripts written for the old scene as much as possible.


There are two different scripts 'A_Player' and 'B_Player' which share few content. Now I would like an old script 'Info' to be able to handle both types of player in the same variable 'player'. But I don't want to use inheritance since then the fictitious base class 'Player' would have to have all the variables public which any script which is supposed to handle both player types could possibly use. This would make a gigantic mess in my code.


I basically want to change the type of a variable 'player' in 'Info' (at all occurences in the code of 'Info') at the time of the scene loading based on which Scene is currently active. I don't want to have e.g. a seperate variable for both types of player and check which one isn't null at all occurences or similar inelegant solutions.


I feel like I am missing something here but I thought about this a lot and couldn't come up with a pleasing solution. I hope you guys can help me.


Greetings,

Daniel

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

2 Replies

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

Answer by scottishrob13 · Jul 28, 2020 at 10:33 PM

What's the problem with inheritance, exactly? Aren't those public fields already public in A_Player and B_Player anyway? With inheritance, you could plop whichever player you want into there. It's designed to solve the problem you're having.


If you don't want to do it that way, you could always make an Interface (see: https://www.tutorialsteacher.com/csharp/csharp-interface) and have both player classes implement the interface. If you do it that way, your Info variable would be typed to the interface and you can use either player class in there. This would be a pretty unusual way to use interfaces though, so be aware of the maintainability issues that arise from that.


If it's just data that you want to access, I recommend using Unity's Scriptable Objects and filling them out differently per-player (they can also benefit from inheritance), but it doesn't sound like that's what you're trying to do.

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 VanillaJudge · Jul 29, 2020 at 08:57 AM 0
Share

Thank you for your answer.

I guess inheritance does solve my problem and I was just hesitant because of the amount of work needed to restructure my code at this point. I would have only the conflicting variables in the base class and use this only when necessary, else I would use the derived classes as data types whenever possible. I will just chase the rat tail for a day or two and make that happen.

I tried interfaces but I think they would just be an unnecessary extra step and I would have to use inheritance to solve the core problem anyway.

Scriptable Objects are new to me, but I guess I can proceed without them.

Thank you!

avatar image
2

Answer by FullMe7alJacke7 · Jul 28, 2020 at 11:34 PM

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected

Protected variables are accessible to inherited classes while remaining hidden from the rest of your application.

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 VanillaJudge · Jul 29, 2020 at 08:59 AM 0
Share

Thank you for your answer. Sadly I don't understand how this would solve my problem.

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

209 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How can I access both classes from a single type? 1 Answer

public variable is not being updated in script. 0 Answers

Inheriting values from base class in c# 0 Answers

Can I have a variable accessed through the inspector and be accessed by a child script without making it public? 1 Answer

"I am getting a null reference exception error for Pathfollowertest.instance.centerslot.Add(Cube.transform) 0 Answers


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