英文:
(C#) When I play this Method, Unity Just Shut Down So I cannot provide you to Error Code
问题
- 游戏中敌人的生命值达到0时,调用uiSelectUnit.Show()。
void Update()
{
if (!isLive)
return;
gameTime += Time.deltaTime;
if (hasAllEnemiesSpawned && gameTime > 1.0f && enemies.Count == 0)
{
uiLevelUp.Show();
}
}
- 当调用Show()时,激活UI窗口,暂停游戏,并调用Next()。
public void Show()
{
Next();
rect.localScale = Vector3.one;
GameManager.instance.Stop();
}
- 选择三个不同的索引,为每个索引的UnitCard/Draggable组件分配数据并激活卡片。
void Next()
{
foreach (UnitCard unitCard in unitCards)
{
unitCard.gameObject.SetActive(false);
}
int[] ran = new int[3];
while (true)
{
ran[0] = Random.Range(0, unitDataItems.Length);
ran[1] = Random.Range(0, unitDataItems.Length);
ran[2] = Random.Range(0, unitDataItems.Length);
if (ran[0] != ran[1] && ran[1] != ran[2] && ran[0] != ran[2])
break;
}
for (int index = 0; index < ran.Length; index++)
{
UnitData ranUnitData = unitDataItems[ran[index]];
UnitCard ranItem = unitCards[index];
ranItem.GetComponent<UnitCard>().data = ranUnitData;
ranItem.GetComponent<Draggable>().linkedUnitData = ranUnitData;
ranItem.gameObject.SetActive(true);
}
}
private UnitData[] LoadUnitDataFromResources()
{
Object[] loadedUnitData = Resources.LoadAll("Unit/Faction1", typeof(UnitData));
UnitData[] unitDataItems = new UnitData[loadedUnitData.Length];
for (int i = 0; i < loadedUnitData.Length; i++)
{
unitDataItems[i] = (UnitData)loadedUnitData[i];
}
return unitDataItems;
}
在按照上述步骤进行操作并启动游戏后,当调用uiSelectUnit.Show()并执行Next时,Unity突然崩溃并游戏停止... 我想告诉您错误数据,但由于它突然停止,我无法找出原因... 如果您需要完整的脚本来解决此问题,请告诉我。展示三个随机选择的单位,并允许用户选择所需单位。
英文:
First off, I gonna Discribe you to Script I'll trying to.
- When the enemy's health reaches 0 in the game, call uiSelectUnit.Show().
void Update()
{
if (!isLive)
return;
gameTime += Time.deltaTime;
if (hasAllEnemiesSpawned && gameTime > 1.0f && enemies.Count == 0)
{
uiLevelUp.Show();
}
}
2.When Show() is Called, it activates the UI Window, pause the Game, and calls Next().
public void Show()
{
Next();
rect.localScale = Vector3.one;
GameManager.instance.Stop();
}
(+ There seem to be no issues up to step 2. There were no anomalies when tested with other scripts.)
- Select three different indexes, assign data to the UnitCard/Draggable component of each index, and activate the cards.
void Next()
{
foreach (UnitCard unitCard in unitCards)
{
unitCard.gameObject.SetActive(false);
}
int[] ran = new int[3];
while (true)
{
ran[0] = Random.Range(0, unitDataItems.Length);
ran[1] = Random.Range(0, unitDataItems.Length);
ran[2] = Random.Range(0, unitDataItems.Length);
if (ran[0] != ran[1] && ran[1] != ran[2] && ran[0] != ran[2])
break;
}
for (int index = 0; index < ran.Length; index++)
{
UnitData ranUnitData = unitDataItems[ran[index]];
UnitCard ranItem = unitCards[index];
ranItem.GetComponent<UnitCard>().data = ranUnitData;
ranItem.GetComponent<Draggable>().linkedUnitData = ranUnitData;
ranItem.gameObject.SetActive(true);
}
}
private UnitData[] LoadUnitDataFromResources()
{
Object[] loadedUnitData = Resources.LoadAll("Unit/Faction1", typeof(UnitData));
UnitData[] unitDataItems = new UnitData[loadedUnitData.Length];
for (int i = 0; i < loadedUnitData.Length; i++)
{
unitDataItems[i] = (UnitData)loadedUnitData[i];
}
return unitDataItems;
}
After entering the steps as mentioned above and starting the game, when uiSelectUnit.Show() was called and Next was executed, Unity suddenly crashed and the game stopped... I'd like to tell you the error data, but I can't figure out the cause since it suddenly stopped... I'd really appreciate any help!
the data was managed as follows.
enter image description here
(The SelectUnit script is applied to the SelectUnit object, with Cards as its children connected to UnitCard. Data is set to None, as it is designed to connect automatically.)
If you need to full script to solve this Problem, Just tell me.
Display three randomly selected units, and allow the user to choose the desired unit.
答案1
得分: 1
崩溃可能是由函数Next
中的while
循环引起的,该循环永远不会结束:只有当rand[0]
,rand[1]
和rand[2]
不同的时候循环才会结束,但是如果在数组unitDataItems
中的项不多,可能这个条件永远不会成立,循环会一直继续下去,从而导致Unity崩溃。
英文:
The crash may be caused by the while
loop in the function Next
that never ends: the loop ends only if rand[0]
, rand[1]
and rand[2]
are different but, if there aren't many items in the array unitDataItems
, it's possible that that condition never becomes true and the loop continues forever, thus crashing Unity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论