鱼C论坛

 找回密码
 立即注册
查看: 884|回复: 1

[已解决]unity求助

[复制链接]
发表于 2024-6-9 20:30:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;

  4. public class ThingsController_Layer1 : MonoBehaviour
  5. {
  6.     //定义各类精灵
  7.     public List<GameObject> sprites;
  8.     public List<GameObject> things;

  9.     void Start()
  10.     {
  11.         //初始化列表
  12.         sprites = new List<GameObject>();
  13.         things = new List<GameObject>();

  14.         //寻找子对象
  15.         FindChild(this.gameObject);
  16.     }

  17.     //刷新
  18.     void Update()
  19.     {
  20.     }

  21.     //寻找子对象
  22.     void FindChild(GameObject child)
  23.     {
  24.         for (int i = 0; i < child.transform.childCount; i++)
  25.         {
  26.             if (child.transform.childCount > 0)
  27.             {
  28.                 FindChild(child.transform.GetChild(i).gameObject);
  29.             }

  30.             AddChild(child.gameObject);
  31.         }
  32.     }

  33.     //分类子对象
  34.     void AddChild(GameObject child)
  35.     {
  36.         idcard = child.GetComponent<IdCard>();
  37.         if (idcard.id != 0)
  38.         {
  39.             if (idcard.type == "player" || idcard.type == "enemy")
  40.             {
  41.                 sprites.Add(child);
  42.             }
  43.             else
  44.             {
  45.                 things.Add(child);
  46.             }
  47.         }
  48.     }
  49. }
复制代码


报错:
  1. Assets\ThingsController_Layer1.cs(5,14): error CS0101: The namespace '<global namespace>' already contains a definition for 'ThingsController_Layer1'
复制代码
  1. Assets\ThingsController_Layer1.cs(11,10): error CS0111: Type 'ThingsController_Layer1' already defines a member called 'Start' with the same parameter types
复制代码
  1. Assets\ThingsController_Layer1.cs(22,10): error CS0111: Type 'ThingsController_Layer1' already defines a member called 'Update' with the same parameter types
复制代码
最佳答案
2024-6-9 20:30:29
这个报错是因为您的代码中存在重复定义的错误。根据报错信息来看,很可能是因为您在同一个命名空间中重复定义了`ThingsController_Layer1`类的代码。

出现这种情况通常有两种情况:

1. 您可能在其他脚本或者同一个脚本中重复定义了`ThingsController_Layer1`类。请检查您的项目中是否存在同名的类文件,并且确认只保留一个即可。

2. 另外一种情况是,如果您之前曾经修改过脚本的文件名,Unity在编译时会将旧的脚本缓存,从而导致重复定义的错误。解决办法是,尝试删除Unity缓存的脚本,然后重新构建项目。

注意,为了保证代码的可读性和维护性,您还需要对其他地方进行修正:

1. 在C#中,泛型List的初始化需要指定元素的类型,例如 `List<GameObject> sprites = new List<GameObject>();` 和 `List<GameObject> things = new List<GameObject>();`。

2. 在 `AddChild()` 方法中,应该使用 `GetComponent<>()` 的形式,并且你需要声明一个类型来存储获取到的组件,例如 `IDCard idcard = child.GetComponent<IDCard>();`。

3. 在 `AddChild()` 方法中,应该使用 `idcard.type.Equals("player")` 或者 `idcard.type.Equals("enemy")` 来比较字符串相等,而不是使用 `==`。

希望以上解答对您有所帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-9 20:30:29 | 显示全部楼层    本楼为最佳答案   
这个报错是因为您的代码中存在重复定义的错误。根据报错信息来看,很可能是因为您在同一个命名空间中重复定义了`ThingsController_Layer1`类的代码。

出现这种情况通常有两种情况:

1. 您可能在其他脚本或者同一个脚本中重复定义了`ThingsController_Layer1`类。请检查您的项目中是否存在同名的类文件,并且确认只保留一个即可。

2. 另外一种情况是,如果您之前曾经修改过脚本的文件名,Unity在编译时会将旧的脚本缓存,从而导致重复定义的错误。解决办法是,尝试删除Unity缓存的脚本,然后重新构建项目。

注意,为了保证代码的可读性和维护性,您还需要对其他地方进行修正:

1. 在C#中,泛型List的初始化需要指定元素的类型,例如 `List<GameObject> sprites = new List<GameObject>();` 和 `List<GameObject> things = new List<GameObject>();`。

2. 在 `AddChild()` 方法中,应该使用 `GetComponent<>()` 的形式,并且你需要声明一个类型来存储获取到的组件,例如 `IDCard idcard = child.GetComponent<IDCard>();`。

3. 在 `AddChild()` 方法中,应该使用 `idcard.type.Equals("player")` 或者 `idcard.type.Equals("enemy")` 来比较字符串相等,而不是使用 `==`。

希望以上解答对您有所帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 11:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表