- Home /
Trying to make an object invisible while hovering..
hey there, me and a mate are trying to make an object invisible just while hovering, there must be a mistake in the code. we tried our best but we did not figure it out.
please help! thanks a lot!!!
ps.: the 0f actually should be changed into a 0.5f but it doesn't work either... it should be only slightly invisible
using UnityEngine;
using System.Collections;
public class Invisible : MonoBehaviour
{
private Color initialColor;
void Start()
{
initialColor = renderer.material.color;
}
void OnMouseOver()
{
renderer.material.color = new Color(initialColor.r, initialColor.g, initialColor.b, 0f);
}
void OnMouseExit()
{
renderer.material.color = initialColor;
}
}
Answer by DaveA · Dec 26, 2013 at 08:20 PM
Make sure there is a Collider (or RigidBody) on the object. Can be 'trigger' if you don't want to actually collide with it.
I got a box collider arround it... and it is triggered... but it doesn't work either :(
Then see robertu's answer, probably need a Transparent shader.
Answer by robertbu · Dec 26, 2013 at 08:28 PM
Your code looks fine. Best guess is that this is a shader issue. Your code will only work if the shader supports transparency and has a main color attribute. As a starting point, use the Transparent/Diffuse shader. And as @DavidA indicates, the object must have a collider, and that collider must be topmost. If you have other colliders in the way, it will not work.
actually I answered allready :D well ok... I got a box collider and it is triggered. if I am using the transparent/duffuse shader it will be invisible right from the beginning... I don't want it to be invisible all the time :)
$$anonymous$$ake sure the initial alpha value is non-transparent
Using the Transparent/Diffuse should not have the object invisible from the start unless you've edited the color in the material in the Inspector. It will be dark unless you have a light shining on your surface. As a test, do:
Game Object > Other > Directional Light
But it sounds to me like you want a unlit shader with alpha. The one I use came with another package, so I cannot post it, but the Unity Wiki has this shader which should do the job: