Fade In and Out Object

Hi,

I would like to fade an object in and out over time, I’m referencing this script : -

http://wiki.unity3d.com/index.php/FadeObjectInOut

But it keeps throwing up loads of errors, and I’m afraid I’m not technical enough on the programming side to fix them, can anyone else get this working, or offer advice on any alternative methods

I’m using unity 4.0.1 if that means anything, the object is a standard polyplane with a .png texture on it, I’m using mobile > transparent > vertex colour and I literally just want it to fade in and out over a specified time interval.

Hope someone can help.

Thanks.

Why not upgrade? it might help because that shader doesn’t exist or at least isn’t named that in latest version I have…

As far as the code posted in that link, there are several problems… it lists the ‘using’ items twice and there is an extra ‘}’ at the end. In any case, that code doesn’t fade the object in and out. This code fades an object in or out.

try this example code with a primitive object that has a material with a color component…and a shader like Transparency>Diffuse

using UnityEngine;
using System.Collections;

public class LerpAlpha : MonoBehaviour 
{
	public float duration = 1;
	public float alpha = 0;
	Color color;
	
	void Start ()
	{
		color = renderer.material.color;
	}
	
	void Update () 
	{
		lerpAlpha();
	}

	void lerpAlpha () 
	{ 
		alpha = Mathf.PingPong(Time.time, duration) / duration; 
		Color tempcolor = new Color(color.r,color.g,color.b,alpha);
		renderer.material.color = tempcolor;
	}
}

Use Fade instead.

–Eric

ooo nice… will have to play around with this… thanks!

Say, Eric, your screenwipe script is out of date. It did help me to write a simple crossfade, which was all I needed.

What’s out of date about it? I haven’t looked at it in a while.

–Eric

They changed how you can get an image. You have to use OnPostRender or something like that and it has to be on the camera. I had to call a render from my main script and then pick it up and fade it in. Maybe there’s a better way, I don’t know.

Indeed, and I see it uses .active rather than SetActive. Well, it’s quite an old script… I updated the description to note it doesn’t work on Unity 4.

–Eric

Thanks all, those answers are a great help, I’ve got something working now.

I should be able to tweak for my needs, the fade in and out works as required, but ideally I’d like my object to be on screen for some time before fading, so I’m going to look at possibly randomising the fade as I’d like it to apply to a few different objects in the scene, kind of like ‘twinkling stars’ that randomly fade in and out.

The reason I’m still with 4.0.1 is a plugin I’m using for android live wallpaper development is only compatible upto this version, it doesn’t work with more recent versions of unity. :confused:

The object can be on screen as long as you want; it won’t change until you call a Fade function.

–Eric