UnityEngine.Canvas' does not contain a definition for `FindWithTag'
I am trying to instantiate a text object stating the new score when and where an object dies. I can instantiate the object but I can not see it in the game view until it is parented to the canvas. That's where the trouble begins.
private Canvas canvas;
void Start()
{
pos = transform.position ;
Canvas thisCanvas = Canvas.FindWithTag("canvas");
canvas = thisCanvas.GetComponent<Canvas>();
}
public void killBubble () {
Destroy(gameObject);
ScoreManager.score += scoreValue;
Instantiate (scoreText, pos, Quaternion.identity);
scoreText.transform.parent = canvas.transform;
}
how can i find the canvas to make it a parent of the instantiated text object?
thanks in advance for your help
The syntax is GameObject.FindWithTag or gameObject.FindWithTag. You are using [Component].FindWithTag.
You can do this ins$$anonymous$$d
void Start()
{
pos = transform.position;
GameObject go = GameObject.FindWithTag("canvas");
canvas = go.GetComponent<Canvas>();
}
This does require that your canvas is tagged as canvas
(case sensitive).
Answer by txzeenath · Oct 03, 2016 at 07:57 PM
There may be another way, but I always use:
GameObject.FindGameObjectWithTag("myTag")
or
GameObject.FindWithTag("myTag")
(same function)
GameObject myCanvas = GameObject.FindWithTag("Canvas");
myTextGameObject.transform.SetParent(myCanvas.transform);
Should get the object tagged "Canvas" and set myTextGameObject as a child. You shoudn't need to get the canvas component to parent it, since it's always paired to a GameObject.