• 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 fighder · Nov 04, 2015 at 04:35 AM · coroutinerenderertypeconvertingsystem.type

Converting Type to SpriteRenderer/Image/Text

Basically I am trying to make some visual effects like fading out and stuff, and I am using coroutines to run those effects.

When I start the coroutine, I passed down arguments with an object[] like the following:

 if (Input.GetMouseButtonDown (1)) {
             object[] tempParmArray = new object[2];
             tempParmArray [0] = gameObject;
             tempParmArray [1] = false;
             effectScript.StartCoroutine ("flashEffect", tempParmArray);
         }

Then the couroutine will just read each of the elements in the array. However, these visual effects are applied to different components, mostly the UI Image, Text and the standard SpriteRenderer.

Normally when I change the color of an object will do something like this:

 gameObject.GetComponent<SpriteRenderer>().color = "some color";

However, given that the components will switch up from time to time, this line won't all the time, so I am trying to do something like this:

 gameObject.GetComponent<Type.GetType("component name")>().color = "some color";

where component name is a parameter that I will pass onto the coroutine running the code. However, Unity won't let me compile this because it doesn't know what type it will get, and the type it gets might not have ".color".

I tried tricking Unity with something like this:

Type componentType = Type.GetType (componentTypeString);

     SpriteRenderer r = null;

     if (componentType == typeof(SpriteRenderer)) {
         r = target.GetComponent<SpriteRenderer> ();
     } else if (componentType == typeof(Image)) {
         (Image) r = target.GetComponent<Image> ();
     } else if (componentType == typeof(Text)) {
         (Text) r = target.GetComponent<Text> ();
     }

. . . . r.color = "some color";

and this:

 Type componentType = Type.GetType (componentTypeString);
             
             ovject r = null;
     
             if (componentType == typeof(SpriteRenderer)) {
                 r = target.GetComponent<SpriteRenderer> ();
             } else if (componentType == typeof(Image)) {
                 r = target.GetComponent<Image> ();
             } else if (componentType == typeof(Text)) {
                 r = target.GetComponent<Text> ();
             }
     .
     .
     .
     .
     r.color = "some color";

and ofcourse neither works because the first one I can't convert SpriteRenderer to Image or Text, while the other runes the some problem where Unity won't know if "r" will have the ".color" option or not.

It had been suggested to me to do this:

 Type componentType = Type.GetType (componentTypeString);
             
             object r = null;

                 if (componentType == typeof(SpriteRenderer)) {
                 r = target.GetComponent<SpriteRenderer> ();
             } else if (componentType == typeof(Image)) {
                 r = target.GetComponent<Image> ();
             } else if (componentType == typeof(Text)) {
                 r = target.GetComponent<Text> ();
             }

             if (componentType == typeof(SpriteRenderer)) {
                 (r as SpriteRenderer).color =  "some color";
             } else if (componentType == typeof(Image)) {
                 (r as Image).color =  "some color";
             } else if (componentType == typeof(Text)) {
                 (r as Text).color =  "some color";
             }

Which will probably work, but that means I will need to replace every code I have GetComponent with a 3 part if statement, which I think might be demanding on performance (building this on a tablet, so ya :l)

Any ideas?

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

Answer by fafase · Nov 04, 2015 at 07:12 AM

Text and Image both inherits from Graphic, so you can use that for your method to take any UI element dealing with color.

SpriteRenderer is not from the same namespace so you would have to come up with an overload of your method.

My advice, make one method that takes color, modifies it by a value and return the new color. Then make two methods, one taking Graphic the other taking Material and internally use your color changing method.

 public static IEnumerator FadeColor (Material material);
 public static IEnumerator FadeColor (Graphic graphic);
 private static Color SetColor(Color color, float value);
 // or this way
 private static void SetColor(ref Color color, float value);


for instance:

  public static IEnumerator FadeColor (Material material){
      while(material.color.a>0){
           material.color = SetColor(material.color, value);
          yield return null;
     }
 }
 



Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

32 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

Related Questions

Changing two different objects renderer colour 1 Answer

An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type) 1 Answer

Turn Strings[] into vars[]? 1 Answer

Help with player death/respawn coroutine? 1 Answer

URP material converter loading forever/stuck (Unity 2021.2 beta) 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