Version: 2023.1
言語: 日本語
public void SetActive (bool value);

パラメーター

value Activate or deactivate the object, where true activates the GameObject and false deactivates the GameObject.

説明

Activates/Deactivates the GameObject, depending on the given true or false value.

A GameObject may be inactive because a parent is not active. In that case, calling SetActive will not activate it, but only set the local state of the GameObject, which you can check using GameObject.activeSelf. Unity can then use this state when all parents become active.

Deactivating a GameObject disables each component, including attached renderers, colliders, rigidbodies, and scripts. For example, Unity will no longer call the Update() method of a script attached to a deactivated GameObject. OnEnable or OnDisable are called as the GameObject received SetActive(true) or SetActive(false).

using UnityEngine;

public class Example : MonoBehaviour { private GameObject[] cubes = new GameObject[10]; public float timer, interval = 2f;

void Start() { Vector3 pos = new Vector3(-5, 0, 0);

for (int i = 0; i < 10; i++) { cubes[i] = GameObject.CreatePrimitive(PrimitiveType.Cube); cubes[i].transform.position = pos; cubes[i].name = "Cube_" + i; pos.x++; } }

void Update() { timer += Time.deltaTime; if (timer >= interval) { for (int i = 0; i < 10; i++) { int randomValue = Random.Range(0, 2); if (randomValue == 0) { cubes[i].SetActive(false); } else cubes[i].SetActive(true); } timer = 0; } } }