E
E
evgeniy2000232021-09-04 23:55:21
C++ / C#
evgeniy200023, 2021-09-04 23:55:21

Why is the object not written to the variable when searching by tag?

In my game, on the main stage, the canvas is called at startup. I tried to make it so that when switching to another scene and back, it was no longer active. I tried to do it with a tag, but for some reason the variable refuses to write the found object and always returns null. Although with Audio Sorce everything works fine.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AlphaManager : MonoBehaviour
{
[SerializeField] private string SomeTag;
private void Awake()
{
GameObject canvasObj = GameObject.FindWithTag(SomeTag);
if (canvasObj != null)
{
Destroy(gameObject);
}
else
{
gameObject.tag = SomeTag;
}
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question