有一个游戏对象,上面挂着 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. java 使用simpleDateFormat格式化日期 时间.RP

    首先了解一下格式化日志的所有表示. 时间日期标识符: yyyy:年 MM:月 dd:日 hh:1~12小时制(1-12) HH:24小时制(0-23) mm:分 ss:秒 S:毫秒 E:星期几 D:一 ...

  2. dreamweaver cs5 快捷键

    撤销上一步:ctrl + Z: 回复上一步:ctrl + Y: 代码缩进:左下角(应用原格式)

  3. kaggle Data Leakage

    What is Data Leakage¶ Data leakage is one of the most important issues for a data scientist to under ...

  4. Linux基础学习(二)

    前言: 我们在上一节了解了一下linux的硬件组成,虽然也许对具体的东西还不甚了解,但是我们知道了linux下一切皆文件这一特性 我们装好了CentOS7的虚拟机(这个可以看别人教程来装起来,比较简单 ...

  5. [SinGuLaRiTy] 2017-07-22 NOIP2017 模拟赛

    [SInGuLaRiTy-1029] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. <全是看看代码就会的水题,偷个懒不单独写题解了~& ...

  6. 【bzoj2242】: [SDOI2011]计算器 数论-快速幂-扩展欧几里得-BSGS

    [bzoj2242]: [SDOI2011]计算器 1.快速幂 2.扩展欧几里得(费马小定理) 3.BSGS /* http://www.cnblogs.com/karl07/ */ #include ...

  7. 【转】在Windows64位环境下.net访问Oracle解决方案

    源地址:http://www.cnblogs.com/asingna/archive/2012/05/27/2519950.html

  8. P2446 [SDOI2010]大陆争霸

    \(\color{#0066ff}{ 题目描述 }\) 幻想历8012年5月12日深夜,斯普林·布拉泽降下神谕:"Trust me, earn eternal life."克里斯军 ...

  9. linux下将当前目录下的文件名存到一个文本文件里

    如果只是想得到当前目录下(不包括子目录)的相关文件时:ls -l | grep  ".gz$" > 1.txt 如果想得到当前目录下,包括子目录中的相关文件时,应该用find ...

  10. spring boot 下websocket实现的两种方法

    websocket前台实现代码,保存为html执行就好 html代码来自:https://blog.csdn.net/M348915654/article/details/53616837 <h ...