そういうのがいいブログ

アプリ個人開発 まるブログ

アプリ開発覚え書き

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

環境

Unity 2019.3.7f1

はじめに

今回はオブジェクトにおける
・表示、非表示
・見かけ上の表示、非表示
について書きます。

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


コード

・オブジェクト表示
GameObject型の変数.SetActive(true);

・オブジェクト非表示
GameObject型の変数.SetActive(false);

・オブジェクトを見かけ上表示
Renderer型の変数.enabled = true;

・オブジェクトを見かけ上非表示
Renderer型の変数.enabled = false;

具体例

・オブジェクト表示

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

public class test : MonoBehaviour
{
    [SerializeField] private GameObject a;//GameObject型の変数aを宣言 好きなゲームオブジェクトをアタッチ
 
    void Start()
    {
        a.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(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;
    }
}


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

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

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

おわりに

これで表示非表示はばっちり。