html5 api activex <objiect,Unity - Scripting API: GameObject.SetActive

How do you use documentation throughout your workflow? Share your experience with us by taking this survey.

GameObject.SetActive

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Your name

Your email

Suggestion*

Submit suggestion

Switch to Manual

Declarationpublic void SetActive(bool value);

Parameters

value

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

Description

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值