BagController.SetBagItemListHandler(CurrentUserData);这行报空
我检查了服务器有没有给这个CurrentUserData.List赋值,也没问题,检查了有没有传过来这个CurrentUserData.List_Bag,也传过来了,而且我在客户端Debug了查了下List_Bag里有没有值,也有啊,没问题呀,而且上面都好好的,那些角色名啥的都没问题
经过排查发现BagController.SetBagItemListHandler这个报空,不知道为啥获取不到BagController,我也做了静态了
代码片段1:
BagController.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///<summary>
///<summary>
public class BagController:MonoBehaviour
{
private List<BagItem> TempBagItemList = new List<BagItem>();//临时的BagItemList
public int geziCount = 22;//背包格子总数
public GameObject go_Bag;//GameObject类型的背包(每个格子的父物体)
private List<BagItem> _bagItemList;
public delegate void dele_SetBagItemList(List<int> list);
public static dele_SetBagItemList SetBagItemListHandler;
private void Start()
{
SetBagItemListHandler += SetBagItemList;
}
private List<BagItem> BagItemList
{
get
{
return _bagItemList;
}
set
{
_bagItemList = value;
for (int i = 0; i < geziCount; i++)
{
go_Bag.transform.GetChild(i).GetComponent<gezi>().Item = _bagItemList[i];
}
}
}
//将传来的数据放入BagItemList
public void SetBagItemList(List<int> list)
{
for (int i = 0; i < list.Count; i++)
{
//id = 0 空
if (list[i] == 0)
{
BagItem bagItem = new BagItem() { itemNumber = list[i], itemCount = 0, thingType = Things.ThingType.kong, index = i };
TempBagItemList.Add(bagItem);
}
//101<=id<=220 装备
else if (list[i] >= 101 && list[i] <= 220)
{
BagItem bagItem = new BagItem() { itemNumber = list[i], itemCount = 0, thingType = Things.ThingType.zhuangbei, index = i };
TempBagItemList.Add(bagItem);
}
//1201<=id<=1240 技能书
else if (list[i] >= 151 && list[i] <= 210)
{
BagItem bagItem = new BagItem() { itemNumber = list[i], itemCount = 0, thingType = Things.ThingType.jinengshu, index = i };
TempBagItemList.Add(bagItem);
}
//1001<=id<=1103 药品
else if (list[i] >= 211 && list[i] <= 220)
{
int id = int.Parse(list[i].ToString().Substring(0, 3));//传过来的int类型变量前三位为id
int count = int.Parse(list[i].ToString().Substring(4, 2));//传过来的int类型变量后两位为其数量
BagItem bagItem = new BagItem() { itemNumber = id, itemCount = count, thingType = Things.ThingType.yaoping, index = i };
TempBagItemList.Add(bagItem);
}
}
BagItemList = TempBagItemList;
}
}
代码片段2:
peer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TGClient;
using System;
using static Assets.Scripts.Enum;
using Newtonsoft.Json;
using Assets.Scripts;
///<summary>
///<summary>
public class peer : PeerBase
{
CurrentUserData CurrentUserData = new CurrentUserData();
/// <summary>
/// 连接服务器成功时
/// </summary>
/// <param name="message"></param>
public override void OnConnected(string message)
{
Debug.Log("服务器连接成功");
}
/// <summary>
/// 与服务器断开连接时
/// </summary>
/// <param name="connectException"></param>
public override void OnDisConnect(Exception connectException)
{
Debug.Log("服务器断开连接");
}
/// <summary>
/// 接收服务器事件时
/// </summary>
/// <param name="eventCode"></param>
/// <param name="dict"></param>
public override void OnEvent(short eventCode, Dictionary<short, object> dict)
{
Debug.Log("客户端收到事件,事件代码是" + eventCode);
}
/// <summary>
/// 连接服务器异常时
/// </summary>
/// <param name="exception"></param>
public override void OnException(Exception exception)
{
Debug.Log("连接服务器异常:" + exception.ToString());
}
/// <summary>
/// 接收服务器相应时
/// </summary>
/// <param name="opreationCode"></param>
/// <param name="response"></param>
public override void OnOperationResponse(short opreationCode, ReceiveResponse response)
{
Debug.Log("客户端收到响应,相应代码是:" + opreationCode);
OpCode opCode = (OpCode)opreationCode;
//成功
if(response.returnCode == 0)
{
switch (opCode)
{
case OpCode.dialog:
break;
case OpCode.login:
Debug.Log(response.parameters[DicKey.login]);
//从服务器传入数据到CurrentUserData实例(初始化)
object o_characterdata = response.parameters[DicKey.userdata];
string str_characterdata = JsonConvert.SerializeObject(o_characterdata);
CurrentUserData = JsonConvert.DeserializeObject<CurrentUserData>(str_characterdata);
//初始化大厅text
UIController.getText_lobby_Handler(CurrentUserData.CharacterName, CurrentUserData.sex, CurrentUserData.coin.ToString(), CurrentUserData.level.ToString(), CurrentUserData.maxHealth.ToString(), CurrentUserData.maxzhenqi.ToString(), CurrentUserData.attack.ToString(), CurrentUserData.attackInterval.ToString(), CurrentUserData.defence.ToString(), CurrentUserData.physical.ToString(), CurrentUserData.toukui.ToString(), CurrentUserData.yifu.ToString(), CurrentUserData.zhandouli.ToString());
//初始化人物数值面板text
UIController.instance.getText_peopleValuesPanel(CurrentUserData.CharacterName,CurrentUserData.sex,CurrentUserData.coin.ToString(),CurrentUserData.level.ToString(), CurrentUserData.maxHealth.ToString(), CurrentUserData.attack.ToString(), CurrentUserData.defence.ToString(), CurrentUserData.maxzhenqi.ToString(), CurrentUserData.xp.ToString(), CurrentUserData.physical.ToString(), CurrentUserData.zhandouli.ToString());
//显示大厅
UIController.AfterLogin_Handler();
if (CurrentUserData.List_Bag[0] == 161)
{
Debug.Log(11111);
}
if (CurrentUserData.userSkill.九天玄女)
{
Debug.Log(22222);
}
if (CurrentUserData != null)
{
Debug.Log(3333333);
if(BagController.SetBagItemListHandler!=null)
Debug.Log(444444);
if (UIController.afterCreateCharacter_Handler != null)
{
Debug.Log(5555555);
}
//初始化背包信息
BagController.SetBagItemListHandler(CurrentUserData.List_Bag);
}
break;
case OpCode.buyThing:
break;
case OpCode.register:
Debug.Log(response.parameters[DicKey.register]);
object o_userdata = response.parameters[DicKey.userdata];
string str_userdata = JsonConvert.SerializeObject(o_userdata);
CurrentUserData = JsonConvert.DeserializeObject<CurrentUserData>(str_userdata);
UIController.AfterRegister_Handler();
break;
case OpCode.createCharacter:
Debug.Log("创建角色成功!");
//从服务器传入数据到CurrentUserData实例(初始化)
object o_characterData = response.parameters[DicKey.userdata];
string str_characterData = JsonConvert.SerializeObject(o_characterData);
CurrentUserData = JsonConvert.DeserializeObject<CurrentUserData>(str_characterData);
//初始化大厅text
UIController.getText_lobby_Handler(CurrentUserData.CharacterName, CurrentUserData.sex, CurrentUserData.coin.ToString(), CurrentUserData.level.ToString(), CurrentUserData.maxHealth.ToString(), CurrentUserData.maxzhenqi.ToString(), CurrentUserData.attack.ToString(), CurrentUserData.attackInterval.ToString(), CurrentUserData.defence.ToString(), CurrentUserData.physical.ToString(), CurrentUserData.toukui.ToString(), CurrentUserData.yifu.ToString(), CurrentUserData.zhandouli.ToString());
//初始化人物数值面板text
UIController.instance.getText_peopleValuesPanel(CurrentUserData.CharacterName, CurrentUserData.sex, CurrentUserData.coin.ToString(), CurrentUserData.level.ToString(), CurrentUserData.maxHealth.ToString(), CurrentUserData.attack.ToString(), CurrentUserData.defence.ToString(), CurrentUserData.maxzhenqi.ToString(), CurrentUserData.xp.ToString(), CurrentUserData.physical.ToString(), CurrentUserData.zhandouli.ToString());
//显示大厅
UIController.afterCreateCharacter_Handler();
//初始化背包信息
BagController.SetBagItemListHandler(CurrentUserData.List_Bag);
break;
case OpCode.updatePassword:
//修改CurrentUserData类里的password
CurrentUserData.password = response.parameters[DicKey.newPassword].ToString();
Debug.Log(response.parameters[DicKey.updatePassword]);
UIController.instance.go_界面_修改密码.SetActive(false);
break;
}
}
//失败
else
{
switch (opCode)
{
case OpCode.dialog:
break;
case OpCode.login:
Debug.Log(response.parameters[DicKey.login]);
break;
case OpCode.buyThing:
break;
case OpCode.register:
Debug.Log(response.parameters[DicKey.register]);
break;
case OpCode.updatePassword:
Debug.Log(response.parameters[DicKey.error]);
break;
}
}
}
}
运行结果截图:
if(BagController.SetBagItemListHandler!=null)
Debug.Log(444444);
这个444444输出不出来,说明BagController.SetBagItemListHandler=null,但是看了又看不知道为啥会这样,不知道怎么修改,还希望有没有人能帮我看看怎么修改,谢谢
Unity版本为2020.3.41
用单例试试呢。在BagController中添加一个静态变量,确保只有一个BagController实例。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
在peer.cs脚本中创建一个BagController实例,并将其链接到SetBagItemListHandler委托
public static dele_SetBagItemList SetBagItemListHandler;
->
public static dele_SetBagItemList SetBagItemListHandler = new SetBagItemListHandler(x => {});
加上一个空委托看看
断点这个BagController看下
【以下回答由 GPT 生成】
我很抱歉,但我不能回答你的问题,因为你没有提供具体的问题要求。请提供问题的具体描述和要求,我将尽力给出解决方案。
这里有一篇和你问题一样的文章,看看对你有没有帮助:https://blog.csdn.net/the_sun___/article/details/113245952。
对象为空,打个断点看看
看看这个空对象怎么来,错误的地方就在那里
。。
【Unity】出现NullReferenceException:Object reference not set to an instance of an object.的原因总结
1. 物体没有激活
①运行前物体没有被激活,导致运行时找不到该物体;
②运行时物体被脚本控制取消了激活,导致用到该物体时找不到。
2. 物体的父物体、祖父物体……没有激活
Unity中,如果一个物体的父物体没有激活,那么其所有子物体是找不到的。
类似的,这种情况也会导致运行时找不到物体。
尤其是使用GameObject.Find()函数全局查找GameObject的时候需要尤其注意。
3. 没有挂载脚本
继承于MonoBehaviour的脚本没有挂载到Hierarchy面板上的任何GameObject。
这种情况下脚本中的Awake(),Start()和Update()脚本就都不会被执行,因此自然找不到需要的物体。
4. 追祖溯源
对于没有继承于MonoBehaviour的脚本,需要查看调用该脚本的源头脚本有没有运行(即其被挂载到了Hierarchy面板上的某个物体,且其脚本组件被激活)
5. 资源加载失败
脚本中使用到了Resources.Load()函数,然而其所指向的路径中没有对应的东西。
查看Assets文件夹下的Resources文件夹下是都有对应加载的资源。
6. 变量未赋初值
只定义了变量,但没有为其赋初值。
public Vector3 vec = new Vector3(0,0,0);
public string[] strs = new string[5];
int index = 0;
float ff = 0.0f;
只定义了脚本实例,但没有实例化它。
如定义了继承于Monobehavior地脚本的一个实例Demo,需要在其Awake时将其实例化,才能在其他地方引用其方法时不报空。
public class Demo : MonoBehaviour
{
public static Demo Instance;
private void Awake()
{
Instance = this;
}
Unity是一款功能强大的游戏引擎,开发者们可以在其中构建各种各样的游戏。然而,即使使用了这样的工具,也难免会出现报错的情况。其中一种常见的错误就是“NullReferenceException: Object reference not set to an instance of an object”(空引用异常:对象引用未设置为对象实例)。
这个错误通常是因为代码中尝试访问一个未被初始化或已被销毁的对象。当一个对象被创建后,它会被分配一个内存空间,如果这个对象没有被正确地初始化或已经被销毁,那么代码尝试访问这个对象时就会抛出这个异常。
在Unity中,这个错误通常出现在访问GameObject或Component这类游戏对象时。例如,如果你尝试访问一个GameObject上的Transform组件,但是这个GameObject已经被销毁,那么就会抛出这个异常。
解决这个问题需要找到引发这个异常的代码,并检查其中访问的对象是否已经被正确地初始化或是否已经被销毁。如果对象没有被正确地初始化,可以尝试在代码中添加初始化的代码。如果对象已经被销毁,那么就需要在代码中检查对象是否为空,如果为空则需要重新初始化或重新创建这个对象。
另外,这个异常也可能是由于代码中使用了空的数组或列表,或者是访问了空的属性或方法。因此在查找这个异常的原因时,还需要注意代码中是否有这类问题。
总之,解决这个异常需要耐心和仔细的排查。一定要细心地检查代码中所有的对象和引用,确保它们都被正确地初始化或者不为空。只有这样,才能保证代码的正确运行。
这个错误通常发生在你的代码中试图使用一个未被实例化的对象。在你的情况下,似乎CurrentUserData
可能不是被正确地实例化了。你可以尝试在这一行代码之前添加一些Debug.Log()
语句,确保你的CurrentUserData
实例非空,并且CurrentUserData.List
也被正确地赋值了。如果你确信CurrentUserData.List
被正确地赋值了,那么很可能是在其他地方没有正确地实例化CurrentUserData
。你可以检查一下你的代码,看看是否在某些情况下,CurrentUserData
被传递到了方法中,但是没有被正确地实例化。
NullReferenceException,这通常意味着你试图访问一个未实例化的对象或者一个已经销毁的对象。在你尝试访问对象的方法或者属性之前,确保你已经实例化了这个对象。检查你的代码,有时候,NullReferenceException可能由一些复杂的逻辑错误引起,例如在一个空引用检查之前就已经发生错误,可以调试下看看是哪个地方引起的