【发布时间】:2019-09-09 07:29:35
【问题描述】:
我希望我的列表“questPartyMembers”的成员使用 Debug.LogFormat 方法进行所有输出。我不确定如何进行。我只用一个成员尝试过一次,那个成员已经用完了,但我不知道如何继续。我总是遇到异常:
ArgumentOutOfRangeException:索引超出范围。一定是 非负数且小于集合的大小。参数名称: index System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument 参数,System.ExceptionResource 资源) (在 :0) System.ThrowHelper.ThrowArgumentOutOfRangeException () (在 :0) System.Collections.Generic.List`1[T].get_Item(System.Int32 索引)(在 :0) TheChoosen.Update () (at 资产/TheChoosen.cs:124)
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class TheChoosen : MonoBehaviour
{
public string weaponType;
public bool haveWeapon = false;
public string characterType = "Farmer";
public string firstName;
public bool allGood;
public int gold;
public bool hasDungeonKey = false;
public int progress = 0;
public List<String> questPartyMembers;
void Start()
{
questPartyMembers = new List<String>() { "Grim the Barbarian", "Merlin the Wise", "Sterling the Knight" };
firstName = "Robert";
gold = 10;
weaponType = "dagger of Sabbatooth";
GenerateCharacter();
}
public void GenerateCharacter()
{
Debug.Log("Hello " + firstName);
Debug.Log("Ready to go on an adventure?");
Debug.Log("Type y to answer with yes!");
}
public void startAdventure()
{
Debug.Log("Let's go");
progress++;
}
public void theAdventure()
{
Debug.Log("Your current Gold:" + gold);
Debug.Log("Look around? (s)");
progress++;
}
public void look01()
{
if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log("You're in front of a big oak tree. " +
"There is a large chest on the floor. " +
"Looks like it can be opened easily.");
Debug.Log("Open Chest? (o)");
progress++;
}
}
public void OpenFstChest()
{
if (Input.GetKeyDown(KeyCode.O))
{
Debug.Log("The box opens loudly creaking and reveals the " +
"view of a large, strangely shining dagger.");
Debug.Log("Take the weapon with 't'.");
progress++;
}
}
public void GetWeaponOne()
{
if (Input.GetKeyDown(KeyCode.T))
{
haveWeapon = true;
Debug.Log("You now have the " + weaponType);
Debug.Log("Show QuestPartyMembers? (Q)");
progress++;
}
}
void Update()
{
switch (progress)
{
case 0:
if (Input.GetKeyDown(KeyCode.Y))
{
startAdventure();
}
break;
case 1:
theAdventure();
break;
case 2:
look01();
break;
case 3:
OpenFstChest();
break;
case 4:
GetWeaponOne();
break;
case 5:
if (Input.GetKeyDown(KeyCode.Q))
{
Debug.LogFormat("Members: {0}", questPartyMembers[0], questPartyMembers[1], questPartyMembers[2], questPartyMembers[3]);
}
break;
}
}
}
【问题讨论】:
-
请仅发布重现您的问题的相关代码。你的异常发生在哪里?
-
好的,很抱歉。我的例外发生在我认为的 case-swith 的 case5 中。我认为,c# 没有读取除 questPartyMembers[0] 之外的其他参数,我不知道如何更改:/
-
好把其他的添加到格式字符串
"Members: {0}, {1}, {2} and {3}"或者直接使用字符串插值Debug.Log($"Members: {questPartyMembers[0]}, {questPartyMembers[1]}, questPartyMembers[2] and {questPartyMembers[3]}")