我在构建中测试了我的游戏,但是我收到了一个错误,我没有在编辑器中找到这里是构建图像:https://netane54544-gmail.tinytake.com/media/7608c0?filename=1525514080068_05-05-2018-12-52-30.png&sub_type=thumbnail_preview&type=attachment&width=1198&height=654
此处还有按钮代码:
private void Clicked()
{
int index = 0;
foreach (Gun item in gunSystem.secInventory)
{
Debug.Log(item.gunType);
if (index == ButtonId)
{
if (gunSystem.guns_inInventory[playerScript.keyPress] == false)
{
gunSystem.setGunInvetory(playerScript.keyPress, item.Index.ToString());
Worked = true;
}
}
else
{
index++;
}
}
}
和setGunInvetory代码:
public void setGunInvetory(int n, string gunName)
{
foreach (Gun item in secInventory)
{
Debug.Log(item.gunType);
if (item.gunType == "Normal" && guns_inInventory[playerScript.keyPress] == false)
{
playerGuns[playerScript.keyPress] = Instantiate(Weapon_Normal, Camera.main.transform.position + (Camera.main.transform.forward * Normalview.offSetPosition.z) + (Camera.main.transform.right * Normalview.offSetPosition.x) + (Camera.main.transform.up * Normalview.offSetPosition.y), Quaternion.identity) as GameObject;
playerGuns[playerScript.keyPress].name = gunName;
playerGuns[playerScript.keyPress].transform.parent = gameObject.transform;
playerGuns[playerScript.keyPress].transform.eulerAngles = gameObject.transform.eulerAngles + Normalview.startOffsetRotation;
setGunActive(playerScript.keyPress);
//Store gundata in inventory
item.Named = false;
Inventory[playerScript.keyPress] = item;
guns_inInventory[playerScript.keyPress] = true;
}
else if (item.gunType == "Stride" && guns_inInventory[playerScript.keyPress] == false)
{
playerGuns[playerScript.keyPress] = Instantiate(Weapon_Stride, Camera.main.transform.position + (Camera.main.transform.forward * Strideview.offSetPosition.z) + (Camera.main.transform.right * Strideview.offSetPosition.x) + (Camera.main.transform.up * Strideview.offSetPosition.y), Quaternion.identity) as GameObject;
playerGuns[playerScript.keyPress].name = gunName;
playerGuns[playerScript.keyPress].transform.parent = gameObject.transform;
playerGuns[playerScript.keyPress].transform.eulerAngles = gameObject.transform.eulerAngles + Strideview.startOffsetRotation;
setGunActive(playerScript.keyPress);
playerGuns[playerScript.keyPress].GetComponent<Renderer>().material = Stride_Material;
//Store gundata in inventory
item.Named = false;
Inventory[playerScript.keyPress] = item;
guns_inInventory[playerScript.keyPress] = true;
}
}
}
答案 0 :(得分:1)
OP在评论中提到问题在于这一行
playerGuns[playerScript.keyPress] = Instantiate(Weapon_Normal, Camera.main.transform.position + (Camera.main.transform.forward * Normalview.offSetPosition.z) + (Camera.main.transform.right * Normalview.offSetPosition.x) + (Camera.main.transform.up * Normalview.offSetPosition.y), Quaternion.identity) as GameObject;
将第一个罪魁祸首带到playerGuns
。 NullReferenceException
发生了playerGuns
,因为new Dictionary<TKey, TValue>()
字典仅被声明并且未使用Dictionary<TKey,TValue> playerGuns = new Dictionary<TKey, TValue>();
初始化,因此保持为null而不是初始化的空字典。
这里的解决方案是在使用之前创建字典的新实例:
<body>
<div id="app">
<ul>
<li v-for="text,i in todoList" v-on:click="method_1(i)" >{{text}}</li>
</ul>
<ul>
<li v-for="text,i in todoList" v-on:click="method_2" >{{text}}</li>
</ul>
<ul>
</ul>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script>
new Vue({
el:"#app",
data:{
todoList:["aaaaa",'bbbbbb','ccccc']
},
methods:{
method_1:function(i){
console.log(i) // i is index of v-for
},
method_2:function(e){
console.log(e)
},
}
})
</script>
</body>