IT Learning

実践形式でITのお勉強

Unity

【Unity】error CS0619:’GUIText’ is obsolete:’GUIText has been removed. Use UI.Text instead.’が出た時の対処法

投稿日:2020年7月20日 更新日:

目的

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; を追加

以下ソースコード

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を一から学ぶのにおすすめの本はコチラ

Related

-Unity

執筆者:


  1. […] 【Unity】error CS0619:’GUIText’ is obsolete:’GUIText has been removed. Use UI.Te… […]

【Unity】Standard Assetsでサクッと人間をキー操作する – IT Learning へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です