- Home /
Changing the alpha on outline
Okay, the entire script is a little complex, I'm designing an algorithm, thingy, it's not important however I can't for the life of me figure out how to change the alpha on the outline color of an object.
The code accesses the color of another object via raycast using GetColor
It then stores that in the var outlineColor (Which is a type Color) and changes an object referred to as object (a type Transform)and changes the outline on it like this:
var outlineColor : Color;
var object : Transform;
function Update () {
var up = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if(Physics.Raycast(transform.Find("raycaster").position, up, hit, 100))
{
outlineColor = hit.collider.gameObject.renderer.material.GetColor("_Color");
object.renderer.material.SetColor("_OutlineColor", outlineColor);
}
}
But then I need it to access the new outline color and somehow change it's opacity. Using SetColor worked for actually changing the COLOR but I don't know how to change the alpha (I want it to change to 33, or .32 or something on a 0-1 scale if it matters at all)
Thanks,
Cole
Color.a = Value between 0 and 1 using for alpha color
Try this for change alpha color of object
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour
{
public GameObject myGameObj;
public Color myColor;
void Start()
{
myColor = myGameObj.renderer.material.color;
}
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
myColor.a = (33f/255f); // for alpha color 33
myGameObj.renderer.material.color = myColor;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.B))
{
myColor.a = (34f/255f); // for alpha color 34
myGameObj.renderer.material.color = myColor;
}
}
}
Answer by This-is-Cole · Nov 15, 2013 at 12:53 AM
Ah.... see this is like pretty much what I'm doing I just forgot to actually change the color of the object, I was assuming the answer would be like another bit of code since it wasn't like the main color, which would've been weird but that's why I came here. Thumbs up for your help.
My mistake was that I got the color I was wanting to change, stored as a :Color var, and changed the alpha on that var, which is obviously just a stored reference, not the color on the object itself.
So this is my finished code if anyone wants it
var outlineColor : Color;
var newOutlineColor : Color;
var object : Transform;
function Update () {
var up = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.Find("raycaster").position, up * 100, Color.magenta);
if(Physics.Raycast(transform.Find("raycaster").position, up, hit, 100))
{
outlineColor = hit.collider.gameObject.renderer.material.GetColor("_Color");
object.renderer.material.SetColor("_OutlineColor", outlineColor);
newOutlineColor = object.renderer.material.GetColor("_OutlineColor");
newOutlineColor.a = (7F/255F);
object.renderer.material.SetColor("_OutlineColor", newOutlineColor);
}
}