1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】スクリプトからゲームオブジェクトの表示・非表示を判定する

Last updated at Posted at 2020-06-19

環境

Unity 2019.3.7f1

はじめに

スクリプトからゲームオブジェクトの表示・非表示を判定する方法を説明します。

コード

表示・非表示の判定は
ゲームオブジェクト型の変数.activeSelf
で取得できます。

この関数はbool型の返り値を返すので

bool型の変数=ゲームオブジェクト型の変数.activeSelf;

で使います。

具体例

下記のコードで非表示にしたオブジェクトをアタッチして実行してみます。

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

public class test : MonoBehaviour
{
    [SerializeField] private GameObject a;//GameObject型の変数aを宣言 好きなゲームオブジェクトをアタッチ
    private bool b;//bool型の変数bを宣言
    
    void Start()
    {
        b = a.activeSelf;//aがアクティブかどうか判定し、trueかfalseがbに入る
        Debug.Log("b=" + b);//コンソールに変部bの中身を表示
    }
}

 
結果
変数aにアタッチされていたオブジェクトは非表示だったので変数bの中にはfalseが入っています。
image.png

おわりに

判定した返り値をbool型の変数に入れるのを忘れずに!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?