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 Dolzen · Mar 23, 2018 at 04:22 PM · variablescomponentstypes

Access a component variable with just a type

Hello!

I have this function that adds a item effect to the player, the item effect is a class defined by A in this case:

 public void CallItemEffect<A>()
 {
     player.AddComponent(typeof(A));
 }

Lets suppose that A class (or type) is as follows:

 class itemeffect
 {
     public int id = 0;
 }

And then I can call the function with:

 CallItemEffect<itemeffect>();

That function will add the new component to the player, my question is what if I want to acces the public int id variable within the itemeffect class?

Let's suppose I want to change the CallItemEffect function as follows:

 public void CallItemEffect<A>()
 {
     //If I try this Visual Studio will display an error "The Component class does not have a definition for ´id´"
     player.AddComponent(typeof(A)).id = 90;
 }

That will happen since although typeof(A) is type of 'itemeffect', is being handled like if were type of 'Component' instead.

What would be the correct code to access the id variable? (It needs to be accesed within the CallItemEffect function)

Thanks so much!

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 Vyzier · Mar 23, 2018 at 05:13 PM

Ah.. for this, you need to put a constraint on your generic method. This is done like so:

 public void CallItemEffect<A>() where A : itemeffect
 {
     // code here
 }


You are basically telling the generic method that it could only accept types of type itemeffect and its inheritors (is that what they're called?).

However, you have to change the body to this instead:

 public void CallItemEffect<A>() where A : itemeffect
 {
     player.AddComponent<A>();
 }


Now you can do player.AddComponent<A>().id and it should access itemeffect's id variable.

Hope this helps!

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 Dolzen · Mar 23, 2018 at 06:07 PM 0
Share

This answer is just what I was looking for, thank you. There are a lot of things that I have yet to learn, and this was one of them.

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

77 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

Related Questions

how to get a gameObject's name from another scrpit 1 Answer

Access to components and their variables in Array. 0 Answers

Check if Object is of Type Component 2 Answers

List of reserved variables such as transform and rigidbody 1 Answer

Activate line just one time 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