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 GameDev_Chuck · Aug 31, 2015 at 05:34 PM · textuser interfaceoutline

Is it possible to change Outline at runtime?

Hello, I'm attempting to change the effect distance of an Outline component that I've attached to a UI Text object at run time. I've attached the following simple script to the Text object which should theoretically reduce the effect distance of an outline over a small period of time.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class OutlineXAdjust : MonoBehaviour
 {
     public float speed = 10;
     private Outline outline;
 
     void Start()
     {
         outline = GetComponent<Outline>();
     }
 
     void Update()
     {
         outline.effectDistance.Set(Mathf.MoveTowards(outline.effectDistance.x, 0, Time.deltaTime * speed), 0);
         Debug.Log(outline.effectDistance.x);
     }
 }

For some magical reason however, nothing happens, and the debug continually logs the starting value of outline.effectDistance.x I've tried adjusting the effectColor as well but have had no success there either. Anyone have any idea what the problem is? Many thanks in advance

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
2
Best Answer

Answer by Mikilo · Aug 31, 2015 at 05:38 PM

Hello!

Because effectDistance is a struct.

Replace your Update()

  void Update()
  {
      outline.effectDistance = new Vector2(Mathf.MoveTowards(outline.effectDistance.x, 0, Time.deltaTime * speed), 0);
      Debug.Log(outline.effectDistance.x);
  }

You are welcome. (4 minutes to answer? Not my best score...)

Comment
Add comment · Show 6 · 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 GameDev_Chuck · Aug 31, 2015 at 09:02 PM 1
Share

You sir, are the man! Thanks so much for helping me out with that. I was also able to apply that approach to the effectColor:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class HueShiftTextOutline : $$anonymous$$onoBehaviour
 {
     public Color[] colors;    //set your colors up in the inspector
     public float changeColorTime = 0.5f;
     public int currentIndex = 0;
 
     private int nextIndex;
     private float lastChangeTime;
     private float timer = 0.0f;
     private Outline outline;
     private Color tempColor;
 
     void Start()
     {
         outline = GetComponent<Outline>();
 
         if (colors == null || colors.Length < 2)
         {
             Debug.Log("Need to setup colors array in inspector");
         }
 
         nextIndex = (currentIndex + 1) % colors.Length;
     }
 
     void Update()
     {
         timer += Time.deltaTime;
 
         if (timer > changeColorTime)
         {
             currentIndex = (currentIndex + 1) % colors.Length;
             nextIndex = (currentIndex + 1) % colors.Length;
             timer = 0.0f;
         }
 
         tempColor = colors[currentIndex];                                                                    // Hard swtich between colors
         //tempColor = Color.Lerp(colors[currentIndex], colors[nextIndex], timer / changeColorTime);            // Smooth transition between colors
         outline.effectColor = new Color(tempColor.r, tempColor.g, tempColor.b);
     }
 }

Credit where it belongs, much of this script was adapted from a post made by roberbu

I'm curious though, why does it being a struct cause the issue?

avatar image Mikilo · Sep 01, 2015 at 03:37 AM 1
Share

Pretty clear and simple.

A struct is a value-type, when you use Set it will override fields of a COPY of that struct.

Therefore, it works the same way for transform.position, localPosition, rotation, etc...

Have a good day mister Chuck!

avatar image GameDev_Chuck · Sep 01, 2015 at 05:08 PM 0
Share

Ah, that makes sense. I guess I must have misinterpreted the description of the Set function. When would be an appropriate time/place to use Set? Thanks again for your words of wisdom :)

Also, for future viewers, I found this website to be very helpful in understanding the difference between value types and reference types.

avatar image Mikilo · Sep 01, 2015 at 05:17 PM 1
Share

You can use Set. That is not the problem, let me show you.

 Vector3 pos = transform.position;

 pos.Set(1F, 2F, 3F);

 transform.position = pos;

It seems obviously not suitable... but who knows. It seems that Set will assign new values to a copy when using directly on transform.position.

avatar image GameDev_Chuck · Sep 01, 2015 at 06:52 PM 0
Share

So with

 Vector3 pos;
 pos.Set(1F, 2F, 3F);

the difference between

 transform.position = pos; 

and

 transfrom.postion.Set(1F, 2F, 3F);

would be that in the latter you're only modifying the reference and therefore not modifying the actual position (/value?) of the object?

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

29 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

Related Questions

Strok/Outline around TextMesh using Shader? 0 Answers

Finding all text labels in a scene 1 Answer

How do you create text that can be the child of a non-canvas object? 1 Answer

Text with outline 3 Answers

How do I get the Text Mesh Pro UGUI working in a prefab? 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