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 hernan_arga · Apr 09, 2020 at 09:48 PM · ienumeratorlogicpolymorphism

how can avoid repeat logic in these methods

i have these 2 methods that have the same behavior and i want to make a single function Opacador that can change the color as much for the text as for the image. The problem is that i cannot replace the signature for IEnumerator Opacador (float ... ,Gameobject objeto) because Gameobject can't do Color colorActual = objeto.color. I also tried Color colorActual = gameObject.GetComponent<Renderer>().material.color but this gave me error. is there anything i can do?

alt text

123.png (24.9 kB)
Comment
Add comment · Show 1
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 JxWolfe · Apr 09, 2020 at 09:58 PM 0
Share

hate images

1 Reply

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

Answer by JxWolfe · Apr 09, 2020 at 10:02 PM

 IEnumerator Opacador(float timpDeColorado, float aphaFinal, Image objeto) {
 //Opacador code
 }
 
 IEnumerator Opacador(float timpDeColorado, float aphaFinal, TextMeshProUGUI objecto) {
 //OpacadorDeTilulo code
 }

this is how you take multiple arguments. Like how Destroy(gameObject) and Destroy(gameObject, time). This means that when Opacador gets called, depending on what arguments you feed it will determine how which IEnumerator gets run.


Alternatively, you could use

 IEnumerator Opacador(float timpDeColorado, float aphaFinal, bool useImage, GameObject item) {
    //simalarities
   if(useImage) {
     Image img = item.GetComponent<Image>();
     // image only code
   }
   else {
     TextMesPro txt = item.GetComponent<TextMeshPro>();
     //TextMeshPro only code
   }
 }

This could also work. However, it would be worse when you call it. (overcomplicated)

Comment
Add comment · Show 3 · 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 hernan_arga · Apr 09, 2020 at 10:24 PM 0
Share

But in the first case you're also repeating logic that it is what i want to avoid. Do you think that is better to repeat logic ins$$anonymous$$d of overcomplicated the method? (because the second option is not very OOP)

avatar image JxWolfe · Apr 09, 2020 at 11:43 PM 0
Share

Sorry for not getting back to you, ran into a space issue and somehow firefox reset. (idk) Option A is far superior to option B. It's better to repeat logic than try to round about it. The excess code will really only take up storage space, it won't dampen processing power, (option B would). If you use #region and #endregion tags, they won't even need to display when you are coding. Option A is how unity's default functions are written, and I would follow suit, even if it's just for consistency.

avatar image hernan_arga JxWolfe · Apr 14, 2020 at 04:14 PM 0
Share

thanks for the replying!

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

129 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Why Dose not this work (IEnumerator) 1 Answer

Changing a string in a loop each second 3 Answers

Images don't load after building. 1 Answer

Why is it that when I pass this variable to an IEnumerator, it stops working? 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