IT Learning

実践形式でITのお勉強

Unity

【Unity】The way to fix error CS0619:’GUIText’ is obsolete:’GUIText has been removed. Use UI.Text instead.’

投稿日:

Overview

There occured error CS0619:’GUIText’ is obsolete:’GUIText has been removed. Use UI.Text instead.’ to use Standard Assets. This article shows a way to fix it.

Environment

Unity 2019.4.3f1

Occurences

Making C# script from Assets > Create > C# Script .

Attaching the script to Sphere object by drag & drop, I get message ‘Can’t add script’ .

This massage warns that the name of the script is different from its class name or there are some error of compiling. But I don’t find any error for compiling or the difference of the names.

Now, finding these GUIText error in console tab.

The way to fix it

Double-clicking the error in the console tab then the script “SimpleActivatiorMenu.cs” is opended.

Change it like following.

Addition : using UnityEngine.UI; 
Comment out : public GUIText camSwitchButton;
Addition : public Text camSwitchButton;

This is a sample code fixed.

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;
        }
    }
}

After changing codes and save it, the script can be attached to the Sphere Objects.

Please try it if you get the same problem.

Related

-Unity

執筆者:


comment

Your email address will not be published. Required fields are marked *