-2

Hi Having trouble with the following coroutine part of my script in c#. The error message I get is attached in the screen grab, seems to be the: GetComponent<Renderer>().

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class test: MonoBehaviour
{
    public float model2;
    public float model1;

    public void section1()
    {
        SceneManager.LoadScene("section1", LoadSceneMode.Single);
    }

    public void fade()
    {
       StartCoroutine(colorlerpin());
    }

    public IEnumerator colorlerpin()
    {    
        float ElapsedTime2 = 0.0f;
        float TotalTime2 = 1f;
        while (ElapsedTime2 < TotalTime2)
        {
            //  fades out main heart
            ElapsedTime2 += Time.deltaTime;
            model1.GetComponent<Renderer>().material.color = Color.Lerp(new Color(1f, 1f, 1f, 1f), new Color(1f, 1f, 1f, 0f), (ElapsedTime2 / TotalTime2));
            yield return null;
            //  fades in cutaway
            ElapsedTime2 += Time.deltaTime;
            model2.GetComponent<Renderer>().material.color = Color.Lerp(new Color(1f, 1f, 1f, 0f), new Color(1f, 1f, 1f, 1f), (ElapsedTime2 / TotalTime2));
            yield return null;
        }
    }
}

enter image description here

derHugo
  • 83,094
  • 9
  • 75
  • 115
jono
  • 35
  • 8
  • 5
    Please do not post screenshots of your code and/or error messages (something to read: [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/a/285557/2819245), [An image of your code is not helpful](http://idownvotedbecau.se/imageofcode) and [Pictures of exceptions are not helpful](http://idownvotedbecau.se/imageofanexception/)). –  Feb 19 '19 at 12:16
  • ^this. Also you should propably read some basic naming conventions and try to apply them to your code. Function and class names are usually written in PascalCase as an example. Not respecting this can lead to some problems for you and confusion for people trying to help you. Read [this](https://learn.microsoft.com/de-de/dotnet/standard/design-guidelines/naming-guidelines) for example. – Nicolas Feb 19 '19 at 12:28

1 Answers1

5

The image tells you what the problem is: you are trying to call GetComponent on a field that is of type float, not a GameObject or MonoBehaviour.

floats are plain numbers like 0.5 or 2, they do not have components. You may want to change the type to GameObject / MonoBehaviour

derHugo
  • 83,094
  • 9
  • 75
  • 115
zambari
  • 4,797
  • 1
  • 12
  • 22