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 /
avatar image
0
Question by keburanuil · May 07, 2014 at 06:43 PM · javascriptobjecttypepropertiescast

Accessing properties from variable

 private var rend;
 
 function Awake()
 {
     if (gameObject.GetComponent(TextMesh) != null)
         rend = gameObject.GetComponent.<TextMesh>();
 
     else if (gameObject.GetComponent(SpriteRenderer) != null)
         rend = gameObject.GetComponent.<SpriteRenderer>();
 }

Now if I want to use rend

 rend.color.a = 0.5; // Gives an error: 'color' is not member of 'Object'

So I have to do it in this way:

 (rend cast SpriteRenderer).color.a = 0.5; // if using SpriteRenderer

However, I have to do this maybe 100 times in my script and I have to always check which type I'm using making all this pointless. The idea behind this is to use the same variable to change things regardless what type is placed to gameobject. So I could always use like rend.color.a, because both TextMesh and SpriteRenderer have color properties.

Comment
Add comment · Show 3
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 Kiwasi · Jul 30, 2014 at 12:48 AM 0
Share

Best way forward I can see is to create a wrapper class using reflection. might come back later with an example.

avatar image keburanuil · Jul 30, 2014 at 01:00 AM 0
Share

Example would be really appreciated :)

avatar image Kiwasi · Jul 30, 2014 at 03:12 AM 0
Share

See @SirCrazyNugget's comment to his answer below. $$anonymous$$ore efficient then $$anonymous$$e would be.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SirCrazyNugget · Jul 29, 2014 at 10:43 PM

I'm not sure whether this has been bumped or UA's had a timelapse, either way you'd be better off just storing the reference to Color rather than the GO

 private var color : Color;
 
 function Awake(){
   if(gameObject.GetComponent(TextMesh) != null){
     color = gameObject.GetComponent.<TextMesh>().color;
   }else if(gameObject.GetComponent(SpriteRenderer) != null){
     color = gameObject.GetComponent.<SpriteRenderer>().color;
   }
 
   color.a = 0.5;
 }

Obviously keep the reference to rend too if needed.

Comment
Add comment · Show 10 · 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 keburanuil · Jul 30, 2014 at 12:25 AM 0
Share

It still doesn't work. I get compiler error: 'a' is not a member of 'object'.

avatar image SirCrazyNugget · Jul 30, 2014 at 12:32 AM 0
Share

sorry, missed off the important part.

 private var color : Color;
avatar image keburanuil · Jul 30, 2014 at 12:41 AM 0
Share

I guess you can't store object properties to a variable, then change them and see the change in original properties.

Now it stores color into the variable that you can only read, not change.

avatar image Bunny83 · Jul 30, 2014 at 12:47 AM 1
Share

@SirCrazyNugget: this doesn't work as Color is a value type and not a reference type. When you assign a value type to a variable you copy the value and not a reference.

avatar image SirCrazyNugget · Jul 30, 2014 at 02:10 AM 1
Share

Ahh Cr*p forgot about Color being a value type.

Plan B: How about just creating a simple function which handles the types for you

 function SetAlpha(rend, a : float){
     if(typeof(rend) == Text$$anonymous$$esh) (rend cast Text$$anonymous$$esh).color.a = a;
     if(typeof(rend) == SpriteRenderer) (rend cast SpriteRenderer).color.a = a;
 }
 
 function Start(){
     SetColor(rend, 0.5);
 }

And just create the necessary functions that way?

Show more comments

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

22 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

Related Questions

Is it possible to use a custom object in java (my WeaponObject) as a data type (like Vector3) 1 Answer

javascript to C#, UnityEngine.Object to GameObject? 1 Answer

Classes and type casting? 2 Answers

Cast Static variables as generic ones? 1 Answer

Move object back automatically 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