- Home /
I cant find out what will fix this code it has stumped me for a week now with no solution that I can find
using UnityEngine; using System.Collections; using System;
public class ChangeAlpha : MonoBehaviour {
void Update ()
{
if(Input.GetKeyUp(KeyCode.T))
{
StartCoroutine(FadeTo(0.0f, 1.0f));
}
if(Input.GetKeyUp(KeyCode.F))
{
StartCoroutine(FadeTo(1.0f, 1.0f));
}
}
IEnumerator FadeTo(float aValue, float aTime)
{
float alpha = transform.renderer.material.color.a;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha,aValue,t));
transform.renderer.material.color = newColor;
yield return null;
}
}
} this is my code and the lines that say renderer.material.color give me error code CS1061
Answer by Kciwsolb · May 10, 2018 at 04:37 PM
Try using: float alpha = GetComponent<Renderer>().material.color.a;
And try using: GetComponent<Renderer>().material.color = newColor;
That error occoccurs when you try to call a method or access a class member that does not exist.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1061
In this case, GameObject.renderer was removed in version 2018.1.0. So you must change how you reference it (like I showed you above).
https://docs.unity3d.com/ScriptReference/GameObject-renderer.html
Also make sure you are using a rendering mode on your material (check the top of the inspector when you select your material) that supports transparency. The default is opaque. Change it to something else.
@$$anonymous$$ciwsolb How do I find the material for a 3d model that I downloaded?
Click on your object, find the material in the inspector, if it is using the default standard shader, make sure it is using transparent or fade for the rendering mode.
But anyways, my original answer answers your main question, which was why you were getting an error. So, if you change what I said above, it should compile. The last part of my answer and this response was just a suggestion of a way you could work around that if your material was using the default shader.
Answer by JonPQ · May 10, 2018 at 04:28 PM
Which line is the error, reading color or writing it ? if its reading, then maybe the object you are running this on, does not have a renderer, or material. add some checks in the code to check for nulls Is it a sprite ? or Ui object, most of those have no material.
If it is causing error in writing the color, maybe you have an invalid number in there... print out the color you are trying to write with debug log. (temporarily) before you try assigning it. also try running debugger, and stepping through line by line, checking what all the values are.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162414im_/https://answers.unity.com/themes/thub/images/avi.jpg)