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 /
  • Help Room /
avatar image
0
Question by Svarr · Feb 01, 2016 at 03:32 PM · typesvariable-definition

Is it possible to define a variable of multiple types?

I have a class which contains a var "myRenderer". This class is used in the game and in the UI as well. "myRenderer" is meant to contain the renderer that belongs to the appropriate instance of that class, so that I can easily access the instances visuals.
Now there is the problem, that I 'visualise' (the only front-end manifestation of the class instances is via sprites) some instances inside the UI not the normal game, which forces me to use Images instead of SpriteRenderers.
I don't want to create a second class, that is identical to the existing one (except the type of the myRenderer var), just because I have visualised 5 instances of it inside the UI. I also would have to duplicate every method relying on that class accordingly.
Is there a better solution?

Comment
Add comment · Show 2
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 brunocoimbra · Feb 03, 2016 at 04:05 AM 0
Share

Why do you need to keep the the SpriteRenderer/Image reference if you are not doing anything graphic related inside your class?

avatar image Svarr brunocoimbra · Feb 06, 2016 at 09:00 AM 0
Share

I just replace the sprites. Didn't think that was a big deal.

2 Replies

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

Answer by cjdev · Feb 03, 2016 at 01:31 AM

If I understand what you're looking for correctly I think you could just use a generic method to get your variable. If it's a variable of a type that inherits from Component, as I believe SpriteRenderer and Image do, then both types can be stored there and the generic method will return it back with the type specified, much like the GetComponent method in Unity.

 public class ContainerClass : MonoBehaviour {
 
     public Component myRenderer;    
 
     public T GetMyRenderer<T>()
     {
         return (T)(object)myRenderer;
     }
 
 }
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 Svarr · Feb 06, 2016 at 09:58 AM 0
Share

Thank you very much!

avatar image
0

Answer by hexagonius · Feb 01, 2016 at 10:01 PM

It would work if there was a common base class, but unfortunately that's "object" and therefore not even capable of doing anything graphic related. I don't think there is a way since there's just no common behaviour.

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 Svarr · Feb 02, 2016 at 09:01 PM 0
Share

What do you mean with common base class? The class itself doesn't do anything graphic related. The problem is just that I store the appropriate SpriteRenderer in a variable of the instance of that class, while I cannot assign a SpriteRenderer for every instance, since some have to be UI Images. I need to somehow take that into account.
I mean, I could add a second variable of type Image, but then I had always one unused variable and everytime I want to access the Image/SpriteRenderer, I would have to check first, which one applies.

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

42 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

Related Questions

change the variable of enemy to attack 0 Answers

Using C# and JS together, List types won't agree 0 Answers

How to make a customizable variable in this script? 1 Answer

typemask and type are null 1 Answer

Question About Defining and Calling Static Variables 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