鱼C论坛

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

[已解决]unity求助

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

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

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

x
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThingsController_Layer1 : MonoBehaviour
{
    //定义各类精灵
    public List<GameObject> sprites;
    public List<GameObject> things;

    void Start()
    {
        //初始化列表
        sprites = new List<GameObject>();
        things = new List<GameObject>();

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

    //刷新
    void Update()
    {
    }

    //寻找子对象
    void FindChild(GameObject child)
    {
        for (int i = 0; i < child.transform.childCount; i++)
        {
            if (child.transform.childCount > 0)
            {
                FindChild(child.transform.GetChild(i).gameObject);
            }

            AddChild(child.gameObject);
        }
    }

    //分类子对象
    void AddChild(GameObject child)
    {
        idcard = child.GetComponent<IdCard>();
        if (idcard.id != 0)
        {
            if (idcard.type == "player" || idcard.type == "enemy")
            {
                sprites.Add(child);
            }
            else
            {
                things.Add(child);
            }
        }
    }
}

报错:
Assets\ThingsController_Layer1.cs(5,14): error CS0101: The namespace '<global namespace>' already contains a definition for 'ThingsController_Layer1'
Assets\ThingsController_Layer1.cs(11,10): error CS0111: Type 'ThingsController_Layer1' already defines a member called 'Start' with the same parameter types
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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-24 20:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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