有一个游戏对象,上面挂着 3 个脚本,如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class SaveAndRead : MonoBehaviour { public static SaveAndRead instance; /// <summary>
/// 保存所有 单独的DataStore
/// </summary>
public List<DataStore> allAloneDataStores = new List<DataStore>(); /// <summary>
/// 初始化SaveAndRead类
/// </summary>
public void InitSaveAndRead()
{
if (instance == null)
{
instance = new SaveAndRead();
}
} void Awake()
{
InitSaveAndRead();
GetAll_AreaDataStoresInTrigger();
} void GetAll_AreaDataStoresInTrigger()
{
//拿到所有子物体的 AreaAloneDataStores类
AreaAloneDataStores[] temp_list02 = GetComponentsInChildren<AreaAloneDataStores>();
for (int i = ; i < temp_list02.Length; i++)
{
for (int j = ; j < temp_list02[i].aloneDataStores.Count; j++)
{
allAloneDataStores.Add(temp_list02[i].aloneDataStores[j]);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class DistinguishDataStoresInTrigger : MonoBehaviour { /// <summary>
/// 存放单独游戏对象的类
/// </summary>
AreaAloneDataStores temp_AreaAloneDataStorest;
/// <summary>
/// 存放触发Trigger的所有游戏对象的List
/// </summary>
public List<GameObject> objs = new List<GameObject>(); void Start()
{
//拿到存放单独游戏对象的类
temp_AreaAloneDataStorest = gameObject.GetComponent<AreaAloneDataStores>();
//确保先通过 OnTriggerEnter 拿到游戏对象 , 再调用 GetAllDataStores 拿到游戏对象上的 DataStore
Invoke("GetAllDataStores", 1f);
}
/// <summary>
/// 拿到所有触发Trigger的游戏对象
/// </summary>
/// <param name="other"></param>
void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == )
{
objs.Add(other.gameObject);
other.gameObject.SetActive(false);
}
}
/// <summary>
/// 拿到所有的DataStore
/// </summary>
void GetAllDataStores()
{
for (int i = ; i < objs.Count; i++)
{
//拿到 Trigger 的 DataStoreSaveToTrigger 脚本里面存的 DataStore
DataStore temp_DataStore = objs[i].GetComponent<DataStoreSaveToTrigger>().dataStore;
//Debug.Log(this.name + "--" + objs[i].name);
//判断 DataStore 是否是Alone的,并加到不同的类的List里
if (temp_DataStore.isAloneSave == true)
{
temp_AreaAloneDataStorest.aloneDataStores.Add(temp_DataStore);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class AreaAloneDataStores : MonoBehaviour { /// <summary>
/// 单独的DataStore的List
/// </summary>
public List<DataStore> aloneDataStores = new List<DataStore>();
}

Bug:

代码的意思是通过 DistinguishDataStoresInTrigger 脚本拿到一些游戏对象,然后对这些游戏对象上的 DataStoreSaveToTrigger 脚本里的 DataStore进行分类,DataStore里的isAloneSave是true时,就把 DataStore 放在 AreaAloneDataStores类 的 aloneDataStores 里。

当时是有2个 DataStore 的 isAloneSave 是 true ,在 DistinguishDataStoresInTrigger 脚本 进行判断后,也确实放了2个 DataStore 到 AreaAloneDataStores类 的 aloneDataStores 里,但是在 SaveAndRead脚本 里通过 temp_list02[i].aloneDataStores 来获得AreaAloneDataStores类 的 aloneDataStores 时,里面却一个也没有。

原因:

是 DistinguishDataStoresInTrigger脚本 里的 Invoke("GetAllDataStores", 1f);

因为它所以在 DistinguishDataStoresInTrigger脚本 执行了 Start 方法 1 秒之后,才开始执行 GetAllDataStores 方法,才把 2 个 DataStore 放到 AreaAloneDataStores类 的 aloneDataStores 里。

而 SaveAndRead脚本 在Awake 方法里就通过 GetAll_AreaDataStoresInTrigger 方法拿到了 AreaAloneDataStores类 的 aloneDataStores 。

所以在 SaveAndRead脚本里拿到 AreaAloneDataStores类 的 aloneDataStores 的时候,DistinguishDataStoresInTrigger脚本 还没有把 2 个 DataStore 放到 AreaAloneDataStores类 的 aloneDataStores 里。

解决方法:

把 SaveAndRead类 Awake 方法里的 GetAll_AreaDataStoresInTrigger();  改为 Invoke("GetAll_AreaDataStoresInTrigger", 2f); 就可以了。

那些经历过的Bug Unity的Invoke方法的更多相关文章

  1. Unity Invoke 方法

    Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("a", 5);   它的意思是:5 秒之后调用 a() 方法: 使用 Invoke() 方法需要注意 ...

  2. (@WhiteTaken)Unity中Invoke的用法

    今天无意间读到大神写的代码,看到了Invoke函数,于是产生兴趣.后来才明白自己要学习的东西还有很多. 下面讲用法. Invoke是延时调用函数,在用Invoke函数之前需要引入命名空间using U ...

  3. Unity中Invoke 和 InvokeRepeating的区别

    Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("Test", 5);   它的意思是:5 秒之后调用 Test() 方法: 使用 Invoke() ...

  4. Unity MonoBehaviour.Invoke 调用

    使用 Invoke() 方法需要注意 3点: 1 :它应该在 脚本的生命周期里的(Start.Update.OnGUI.FixedUpdate.LateUpdate)中被调用: 2:Invoke(); ...

  5. JAVA深入研究——Method的Invoke方法。

    在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用父类的对象也会报错,虽然这是很符合多态的现象,也符合java的动态绑定规范,但还是想弄懂java ...

  6. JAVA深入研究——Method的Invoke方法

    http://www.cnblogs.com/onlywujun/p/3519037.html 在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用 ...

  7. JAVA深入研究——Method的Invoke方法(转)

    在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用父类的对象也会报错,虽然这是很符合多态的现象,也符合java的动态绑定规范,但还是想弄懂java ...

  8. 深入解析Java反射-invoke方法

    博客原文:http://www.sczyh30.com/posts/Java/java-reflection-2/ 上篇文章中回顾了一下Java反射相关的基础内容.这一节我们来深入研究Method类中 ...

  9. JAVA深入研究——Method的Invoke方法(转)

    原文地址:http://www.cnblogs.com/onlywujun/p/3519037.html 在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一 ...

随机推荐

  1. c#写对象来读取TXT文本文件

    本博文让你知道怎样写对象,怎样读取文本文件,怎样根据实际条件判断与获取需要的文本行.参考下面网友的问题,根据源文来看,有些行输出的格式,需要把“,”替换为空格. 第一行还附加入后面的子行每一行的后面, ...

  2. 解决:kali linux 在vmware 虚拟机中使用bridge模式上网的问题

    安装kali后,使用独立ip上网,但是设置bridge模式后依然上不了网,后来查了好多资料才解决。 能ping通网页,能ping通DNS,就是不能打开网页。 最后的原因是主机的防火墙拦截,把防火墙关了 ...

  3. Django之QuerySet 创建对象

    在前面的模型介绍中设置了3个对象,出版商(publisher),作者(Authro),书籍(book).首先我们在网页中添加各个对象信息填写的界面.填写后点击提交.将会传递给后端.传递方式采用post ...

  4. ubuntu - 14.04,创建菜单

    我们有的时候可能会把一个执行程序放到一个位置,随后我们希望在ubuntu的菜单里面加入它,这个操作非常简单: 我在Gnome桌面里,选择:“系统工具”->“首选项”->"主菜单& ...

  5. [poj 1185] 炮兵阵地 状压dp 位运算

    Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用&quo ...

  6. ABAP开发常见任务

    在ABAP开发中 最主要的工作: 1 报表的开发 主要使用到数据库读取 ALV LIST等技术: 2 单据的打印 主要使用到数据库读取.SmartFirms.Form等技术: 3 数据的上载 主要使用 ...

  7. docker容器时间不对及java程序时间不对解决

    使用docker容器部署的应用,会出现时间与主机不一致的情况 1. 容器时间与主机差8个小时:主机的与容器的/etc/localtime不一致 解决方法:挂载主机的/etc/localtime,如果没 ...

  8. Django 12 中间件、上下文处理器和admin后台

    Django 12 中间件.上下文处理器和admin后台 一.中间件 #Django中间件 (Middleware) # 一个轻量级.底层的“插件”系统,可以介入Django的请求和响应处理过程,修改 ...

  9. stringstream的用法

    stringstream的基本用法 stringstream是字符串流.它将流与存储在内存中的string对象绑定起来. 在多种数据类型之间实现自动格式化. 1.stringstream对象的使用 # ...

  10. Codeforces ~ 1009B ~ Minimum Ternary String (思维)

    题意 给你一个只含有0,1,2的字符串,你可以将"01"变为"10","10"变为"01","12" ...