But I get the error ‘Assets/BallBehaviour.cs(20,31): error CS1612: Cannot modify a value type return value of `UnityEngine.Material.color’. Consider storing the value in a temporary variable’
Mathf.Lerp would probably work better. To subract Time.deltaTime, wouldn’t you have to set a variable as to what you’re subracting from? In other words, you’re subracting time from zero. At least that’s the way it looks. If you use Lerp, you can control the fade time better and achieve the same result since Lerp uses a range from 0 to 1.
This is what I have now, but it doesn’t start fading at all.
using UnityEngine;
using System.Collections;
public class BallBehaviour : MonoBehaviour
{
Color start;
Color end;
float t = 0.0f;
void Start()
{
start = renderer.material.color;
end = new Color(start.r, start.g, start.b, 0.0f);
}
void Update()
{
float x = transform.position.x;
float z = transform.position.z;
transform.position = new Vector3(x, 0.25f, z);
if (x <= 0 || x >= 10 || z <= 0 || z >= 10)
{
t += Time.deltaTime * 2;
renderer.material.color = Color.Lerp(start, end, t);
if(renderer.material.color.a <= 0.0)
{
Destroy(gameObject);
}
}
//rigidbody.velocity = Vector3.zero;
//transform.position = new Vector3(5.0f, 0.25f, 5.0f);
// Create new ball
}
}
EDIT: Nevermind, I think I think I got it. (I realized why the earlier code wouldn’t work, at least).
EDIT 2: Now it doesn’t actually fade (there’s no color or texture associated with it, is that causing it to not fade?) The ball is destroyed so I know it theoretically works.
As I look into this more, it’s a bit of a complicated area. You need to have a shader with an alpha component, but this isn’t an area I know at all. Here’s a link that might help