c# - Unity 2d:游戏对象集活动不适用于父母和所有孩子
问题描述
我为游戏 2d unity 制作了两个脚本。一个将 bool true 设置为另一个,这是另一个的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManagerScript : MonoBehaviour
{
public float weapons;
public float money;
public bool tutorial = true;
public bool tutorialcoins = false;
public bool tutorialsword = false;
public bool tutorialenemies = false;
// Start is called before the first frame update
void Start()
{
if (tutorial)
{
if (!tutorialcoins)
{
GameObject.Find("Coins").SetActive(false);
}
}
}
// Update is called once per frame
void Update()
{
if (tutorialcoins)
{
GameObject.Find("Coins").SetActive(true);
}
}
所以我使用了那个代码和名为 Coins 的对象,它有很多孩子。当我将 tutorialcoins 设置为 true 时,孩子们和硬币都被激活并且没有任何反应!请问你知道我可能做错了什么吗
解决方案
尝试这个
public Transform coin;
void Start()
{
for (int j = 0; j < coin.childCount; j++)
{
coin.GetChild(j).gameObject.SetActive(false);
}
}
void Update()
{
if (tutorialcoins)
{
for (int j = 0; j < coin.childCount; j++)
{
coin.GetChild(j).gameObject.SetActive(false);
}
}
}
添加此代码后。转到您将此脚本附加到的对象。在您将看到的脚本组件上Coin
。将您的 Coins obj 从层次结构拖动到此并运行。
推荐阅读
- ajax - 从 ajax-call 调用 vb 代码隐藏函数
- r - X 轴上的日期在 R 可视化中不是日期格式
- c# - 如果我在没有等待的情况下调用多个异步方法然后使用 Task.WhenAll,它会是异步的吗?
- javascript - 子组件中未定义回调函数
- spring-boot - spring-boot 在启动期间挂起一段时间
- json - elasticsearch 用于 DWH 可视化的 POC
- regex - 在 json 中获取令牌的正则表达式是什么?
- javascript - Vue JS中通过ID获取静态JSON单品数据
- javascript - 将数据从firestore附加到javascript中的数组
- php - cURL -u 标签,但用于 PHP 中的 SETOPT