c# - 仅向一级子级添加脚本
问题描述
我试图只获取 GameObject 的第一级子级,并为每个第一级子级添加一个脚本。
我收到一个错误:addNavTagChildren.cs(14,21): error CS0161: 'getFirstChildren(Transform)': 并非所有代码路径都返回一个值
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class addNavTagChildren : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
List<GameObject> childrenList = new List<GameObject>();
//TEST
Transform[] getFirstChildren(Transform parent)
{
Transform[] children = parent.GetComponentsInChildren<Transform>();
Transform[] firstChildren = new Transform[parent.childCount];
int index = 0;
foreach (Transform child in children)
{
if (child.parent == parent)
{
firstChildren[index] = child;
index++;
childrenList.Add(child.gameObject);
}
}
}
for (int i = 0; i < childrenList.Count; i++)
{
NavMeshSourceTag nmst = childrenList[i].AddComponent<NavMeshSourceTag>();
}
}
}
解决方案
该行Transform[] getFirstChildren(Transform parent)
是一个方法声明。您是在告诉编译器该方法getFirstChildren
将返回 type 的对象Transform[]
。但是,在您的方法实现中,您不会返回任何内容。这就是错误所抱怨的。如果我正确理解您的代码,您可能打算在循环return firstChildren;
之后添加?foreach
推荐阅读
- html - 如何在div底部的左右两侧放置2个按钮?
- node.js - 在子句Nodejs雪花没有得到结果集
- java - 如何弃用 Google 登录的 startActivityResult
- html - 围绕旋转对象旋转
- php - 使用数据库进行 Laravel 测试 - 每个测试的单个事务未按预期工作
- twitter-bootstrap - 用于 Next.js 应用程序的 Bootstrap 安装 bootstrap 或 react-bootstrap
- java - Springboot 在 POST 请求中添加“_class”字段
- c# - 如何模拟不会导致脆弱测试的实现细节
- amazon-web-services - AWS Database Migration Service 只能迁移几个表或列吗?
- rightnow-crm - rightnow-crm 和不可用时间