首页 > 解决方案 > 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 时,孩子们和硬币都被激活并且没有任何反应!请问你知道我可能做错了什么吗

标签: c#unity3d

解决方案


尝试这个

 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 从层次结构拖动到此并运行。


推荐阅读