【Unity】error CS0619:’GUIText’ is obsolete:’GUIText has been removed. Use UI.Text instead.’が出た時の対処法
目的
UnityでC#スクリプトを記述してゲームオブジェクトにAttachしようとしたときにerror CS0619:’GUIText’ is obsolete:’GUIText has been removed. Use UI.Text instead.’が出たので、その対処方法についてメモする
2020/9 追記
どうやらStandard Assetsを使うときにこのエラーが出てるみたいです。自分の環境ではStandard Assets以外では今のところ起きてないです。
環境
Unity 2019.4.3f1
発生現象
Assets > Create > C# Scriptからスクリプトを作成

作成されたスクリプトファイルをドラッグ&ドロップでSphereオブジェクトに持って行ったところ、ポップアップが現れた。

以下が現れたポップアップ。スクリプトファイル名とクラス名があっていないか、コンパイルエラーがあるとのこと。ファイル名とクラス名の一致を確認したところ問題なし。(ファイルが生成された段階で自動的にファイル名とクラス名は同じになっている)

コンソールタブを確認したところ、以下のエラーが出ていた。どうやらGUITextという記述を使うなとのこと。

対処方法
自分の環境だと、先ほどのエラーをダブルクリックするとVisual Studioが起動し、該当するファイルであるSimpleActivatorMenu.csというファイルが自動で開いた。
このファイルを以下のように編集。
using UnityEngine.UI; を追加
public GUIText camSwitchButton; をコメントアウト
public Text camSwitchButton; を追加
以下ソースコード
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System; using UnityEngine; using UnityEngine.UI; //This is added #pragma warning disable 618 namespace UnityStandardAssets.Utility { public class SimpleActivatorMenu : MonoBehaviour { // An incredibly simple menu which, when given references // to gameobjects in the scene //public GUIText camSwitchButton; //This is comment out public Text camSwitchButton; //This is added public GameObject[] objects; private int m_CurrentActiveObject; private void OnEnable() { // active object starts from first in array m_CurrentActiveObject = 0; camSwitchButton.text = objects[m_CurrentActiveObject].name; } public void NextCamera() { int nextactiveobject = m_CurrentActiveObject + 1 >= objects.Length ? 0 : m_CurrentActiveObject + 1; for ( int i = 0; i < objects.Length; i++) { objects[i].SetActive(i == nextactiveobject); } m_CurrentActiveObject = nextactiveobject; camSwitchButton.text = objects[m_CurrentActiveObject].name; } } } |
上記を変更したところ、無事にドラッグ&ドロップでアタッチできるようになった。

Unityを一から学ぶのにおすすめの本はコチラ
2件のフィードバック
[…] https://obenkyolab.com/?p=1822 […]
[…] 【Unity】error CS0619:’GUIText’ is obsolete:’GUIText has been removed. Use UI.Te… […]