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
2
Question by Zarax · May 13, 2011 at 09:07 AM · alphatextmeshfadecs1612

Fade TextMesh font C#

Hello all,

I'm trying to fade the font of a TextMesh, i can change color font dynamically, but the not the alpa. Here's my code.

public class FadeTextUp : MonoBehaviour {

public Color TextColor = Color.blue;

void Awake() { } // Use this for initialization void Start () { gameObject.renderer.material.color = TextColor; gameObject.renderer.material.color.a = TextColor.a; //error line }

// Update is called once per frame void Update () { gameObject.transform.Translate(0, 0.1f, 0, Space.World); //Here i would put the code for fadding the font ... }

}

But i'm getting this error : Cannot modify a value type return value of `UnityEngine.Material.color'. Consider storing the value in a temporary variable.

What should I do ? Thanks

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by CHPedersen · May 13, 2011 at 09:26 AM

That, or you can grab the material of the MeshRenderer that every TextMesh comes with like so:

GetComponent<MeshRenderer>().material.color = new Color(TextColor.r, TextColor.g, TextColor.b, alpha);

That's what I'm doing. To achieve a fading effect I then Mathf.lerp the alpha value.

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
avatar image
1

Answer by lampshade · May 13, 2011 at 09:18 AM

That error is just like a read only, you cannot modify it. What you want is to use the function SetColor and specifiy what you are trying to do as its first string parameter. For example:

transform.renderer.material.SetColor("_Color", new Color(renderer.material.color.r,renderer.material.color.g,renderer.material.color.b, renderer.material.color.a - 0.75f));

Notice that I am using transform which indicates that i want to change the color of the material the script is attached to.

You can still use gameObject in place of transform as your code shows.

Let me know if that does not do at least something.

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
avatar image
1

Answer by 1Mikhael · Jan 13, 2016 at 10:31 AM

Hi! What about

 var comText = GameObject.FindGameObjectWithTag("comText");

in Update:

 if(timer >= 0)
         {
             comText.GetComponent.<TextMesh>().color.a += 0.4 * Time.deltaTime;
         }
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 BKinAK · Feb 19, 2017 at 08:22 AM 0
Share

This worked for me. Thanks.

avatar image
0

Answer by Zarax · May 13, 2011 at 09:28 AM

Well thanks for your response, I've found a solution at my problem.

public class FadeTextUp : MonoBehaviour {

public Color TextColor = Color.blue; void Awake() { } // Use this for initialization void Start () { gameObject.renderer.material.color = TextColor;

}

// Update is called once per frame void Update () { gameObject.transform.Translate(0, 0.1f, 0, Space.World); TextColor.a = TextColor.a-0.1f; gameObject.renderer.material.color = TextColor;

}

}

Now, it works.

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 Owen-Reynolds · May 13, 2011 at 06:33 PM 0
Share

I call that Pulling Out a variable. Works for most "parts are Read-Only" problems. For example transform.position.x++; is a read-only error, so pull it out: Vector3 POS = tr.pos; POS.x++; tr.pos=POS;. The posts above solve it by setting all parts at once, which is really the same thing, compressed into one step.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Is there a simple way of fading a Texture2D 1 Answer

Fade Shader- One texture fades in, the other fades out 2 Answers

Fading Objects does not work (Unity 5) 0 Answers

Fading all but single instance of a button 1 Answer

How to change guitext alpha value without error cs1612? 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