error CS0103: The name `canvasGroup' does not exist in the current context
I trying to get the text to show and disappear in my scene . When I have save the script I have gotten five of the same errors .
(18,13): error CS0103: The name timeShown' does not exist in the current context (16,6): error CS0103: The name
canvasGroup' does not exist in the current context (17,6): error CS0103: The name textElement' does not exist in the current context (23,6): error CS0103: The name
canvasGroup' does not exist in the current context (24,6): error CS0103: The name `textElement' does not exist in the current context
Here is my script :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class fly : MonoBehaviour {
public Game
void ShowMessage(string message, float timeToShow = 10)
{
StartCoroutine(ShowMessageCoroutine(message, timeToShow));
}
IEnumerator ShowMessageCoroutine(string message, float timeToShow = 10)
{
canvasGroup.alpha = 1;
textElement.text = message;
while (timeShown < timeToShow)
{
timeShown += Time.deltaTime;
yield return null;
}
canvasGroup.alpha = 0;
textElement.text = "The points require for this level is 2500";
}
}
Answer by DiegoSLTS · Aug 31, 2016 at 12:58 PM
Those variables are not defined, that's what the error is saying. It looks like you copy pasted some code, but forgot to copy the definition of the variables. You need something like:
public CanvasGroup canvasGroup;
At the top, inside the class body, and then in the inspector you must drag an object from the scene with a CanvasGroup attached intobthe field that appears for that variable. You need something similar for the other variables. Or check the original source of the code and check there how it's done.
I made all most of the errors go away . I only have one error .
(8,10): error CS0246: The type or namespace name `TextElement' could not be found. Are you missing a using directive or an assembly reference?
I see why it would . How would I declare TextElement ? Here is my code now :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class fly : $$anonymous$$onoBehaviour {
public CanvasGroup canvasGroup;
public TextElement textElement;
void Show$$anonymous$$essage(string message, float timeToShow = 10)
{
StartCoroutine(Show$$anonymous$$essageCoroutine(message, timeToShow));
}
IEnumerator Show$$anonymous$$essageCoroutine(string message, float timeToShow = 10)
{
canvasGroup.alpha = 1;
textElement.text = message;
while (timeShown < timeToShow)
{
timeShown += Time.deltaTime;
yield return null;
}
canvasGroup.alpha = 0;
textElement.text = "The points require for this level is 2500";
}
}
TextElement is not a type, Text is a type and I guess you want that. I think you have to learn about the syntax of C# before doing actual things using Unity features, this are pretty basic things that you're missing, you'll have a lot of this problems if you don't know what a type, a definition and stuff like that really mean.
For example, you have a line that says:
public Game
That won't work, it's like the first part of defining a variable but only that doesn't mean anything. You either remove that or put a name for that variable, and a semicolon at the end, like this:
public Game game;
And that works only if you have a class called "Game" defined somewhere in your project.
If want I to do just U.I text . How do I make it appear on scene like seconds later and disappear in a couple of seconds ?
Answer by importguru88 · Aug 31, 2016 at 04:16 PM
If want to do just U.I text . How do I make it appear on scene like seconds later and disappear in a couple of seconds ?
Answer by $$anonymous$$ · Aug 11, 2018 at 11:12 AM
i do also have the same error. How can we avoid it when importing resources ?