Variable Scope
You must declare the variable MyRenderer
outside of your methods. This will fix your error about MyRenderer not being declared. Declare it like this: private Renderer MyRenderer;
Condition Checking
error CS0029: Cannot implicitly convert type float' to bool'
This error comes from this line of code: if (MyRenderer.material.color.a = 0)
Your operation in if
is using an assigner, =
, not what you would use to check, ==
. I will recommend though, from what it appears you are trying to achieve, you should use variable < 1
so that it will continue to fade in until the color is completely changed.
Changing the Alpha Value
error CS1612: Cannot modify a value type return value of UnityEngine.Material.color'. Consider storing the value in a temporary variable error CS1612: Cannot modify a value type return value of UnityEngine.Material.color'. Consider storing the value in a temporary variable
This comes from the code MyRenderer.material.color.a += FadeTime * Time.deltaTime;
.
You can not change individual values of the 1 individually (the alpha value). Change the value by declaring a new Color and changing its alpha channel, like so:
Color _tempColor = MyRenderer.material.GetColor("_Color");
if (_tempColor.a < 1)
{
MyRenderer.material.SetColor("_Color", new Color(_tempColor.r, _tempColor.g, _tempColor.b, FadeTime * Time.deltaTime));
}
Final
Now you should have a functioning script. In case you still don’t, this is the final script that I came up with after modifying your code.
using UnityEngine;
using System.Collections;
public class Fadestars : MonoBehaviour
{
public float FadeTime = 13f;
private Renderer MyRenderer;
void Start()
{
MyRenderer = GetComponent<Renderer>();
}
void Update()
{
Color _tempColor = MyRenderer.material.GetColor("_Color");
if (_tempColor.a < 1)
{
MyRenderer.material.SetColor("_Color", new Color(_tempColor.r, _tempColor.g, _tempColor.b, FadeTime * Time.deltaTime));
}
}
}