Search code examples
c#animationunity-game-engine2drecipe

How do you Program Text To Fade in Unity (Using c#)?


I am completely new to c# and Unity, so please don't close my question.

I am following a "recipie" to make text fade from a book called Unity 5.x Cookbook. I am pretty sure that I wrote all the code down correctly, but when I try to run my code, the Unity console says this-

Assets/Scripts/fadeScript.cs(10,17): error CS0246: The type or namespace name `CountdownTimer' could not be found. Are you missing a using directive or an assembly reference?

I am new to this, so I don't understand what the problem is. Here is my code-

using UnityEngine;
using System.Collections;

using UnityEngine.UI;
using System;

public class fadeScript : MonoBehaviour {

// Al Variables
private CountdownTimer countdownTimer;
private Text fadeText;
private int fadeDuration = 5;
private bool fading = false;

void Start () {

    fadeText = GetComponent<Text>();
    countdownTimer = GetComponent<CountdownTimer>();
    StartFading(fadeDuration);

}

void Update () {
    if (fading) {

        float alphaRemaining = countdownTimer.GetProportionTimeRemaining();
        print (alphaRemaining);
        Color c = textUI.material.color;
        c.a = alphaRemaining;
        fadeText.material.color = c;

        if (alphaRemaining < 0.01) {

            fading = false;

        }

    }
}

    public void StartFading (int timerTotal) {

        countdownTimer.ResetTimer(timerTotal);
        fading = true;

    }

}

What is wrong with my code? What should I do to fix it? Thanks in advance!

-George

PS- I made sure that the script is connected to the actual text, and that the text is named properly.


Solution

  • Your script doesn't know what a "CountdownTimer" is. Apparently there's no script named "CountdownTimer" in your project. It appears you forgot to add it from your recipe. That script appears to have two functions in it named GetProportionTimeRemaining and ResetTimer.

    PS: Next time copy the error The type or namespace name could not be found. Are you missing a using directive or an assembly reference? into google and you'll get the information you need.