【Unity】スクリプトからのオブジェクト表示・非表示、見かけ上の表示・非表示


見かけ上の非表示というのは
実体はあるけど透明になってしまって見えないという意味です。

コード

・オブジェクト表示

GameObject型の変数.SetActive(true); 下記、具体例

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

public class test : MonoBehaviour
{
    [SerializeField] private GameObject a;//GameObject型の変数aを宣言 好きなゲームオブジェクトをアタッチ

    void Start()
    {
        a.SetActive(true);
    }
}

・オブジェクト非表示

GameObject型の変数.SetActive(false); 下記、具体例

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

public class test : MonoBehaviour
{
    [SerializeField] private GameObject a;//GameObject型の変数aを宣言 好きなゲームオブジェクトをアタッチ

    void Start()
    {
        a.SetActive(false);
    }
}

・オブジェクトを見かけ上表示

Renderer型の変数.enabled = true; 下記、具体例

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

public class test : MonoBehaviour
{
    [SerializeField] private Renderer a;//Renderer型の変数aを宣言 好きなゲームオブジェクトをアタッチ

    void Start()
    {
        a.enabled = true;
    }
}

・オブジェクトを見かけ上非表示

Renderer型の変数.enabled = false; 下記、具体例

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

public class test : MonoBehaviour
{
    [SerializeField] private Renderer a;//Renderer型の変数aを宣言 好きなゲームオブジェクトをアタッチ

    void Start()
    {
        a.enabled = true;
    }
}

オブジェクトがアクティブか非アクティブかを取得する場合

gameObject.activeSelf

参考サイト

https://qiita.com/Maru60014236/items/d542764af1c30c742d9f




この記事が気に入ったらサポートをしてみませんか?