从阵列列表中产生一个数字,显示前面的数字
我需要在Unity中产生一个gameobject,在那里它会产生数字。从阵列列表中产生一个数字,显示前面的数字
对于某些人来说,预制产品的原因是早于应该产生的数字。例如:预制件显示“10”,但实际变量实际上是24.“10”预制件被破坏后,另一个预制件产生“24”。所以基本上,预制被“延迟”了。
using System.Collections; using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HexToBinary : MonoBehaviour
{
//Array 1 - F
public GameObject cvns;
public ArrayList HexBin = new ArrayList();
public GameObject enemy;
public GameObject explosion;
// public GameObject spwnEnemy;
public static int rand1;
public static int rand2;
// public float speed;
public Text result;
public TextMesh question;
public string valGroup1;
public string valGroup2;
public Text totalVar1Txt;
public Text totalVar2Txt;
//Score
// Use this for initialization
void Start()
{
HexBin.Add(1); // 0001
HexBin.Add(2); // 0010
HexBin.Add(3);
HexBin.Add(4);
HexBin.Add(5);
HexBin.Add(6);
HexBin.Add(7);
HexBin.Add(8);
HexBin.Add(9);
HexBin.Add("A");
HexBin.Add("B");
HexBin.Add("C");
HexBin.Add("D");
HexBin.Add("E");
HexBin.Add("F");
//rand1 = Random.Range(0, HexBin.Count - 1);
//rand2 = Random.Range(0, HexBin.Count - 1);
randomSpawn();
//randomizeHex();
// randomizeSpawn();
// spawnExplosion();
}
public void spawnExplosion(Vector3 b)
{
//x, y, z
Instantiate(explosion, b, Quaternion.identity);
// explosion.transform.localPosition = new Vector3(0, 0, 100);
// Debug.Log(enemy.transform.position.y);
Debug.Log("Explotion Spawn");
}
//public IEnumerator enemySpawnTimer()
//{
// yield return new WaitForSeconds(5f);
// //if random matches destroy enemy
// //wait 5 seconds
// //instantiate new enemy
// // randomSpawn();
//}
public void randomizeHex()
{
rand1 = Random.Range(0, HexBin.Count);
rand2 = Random.Range(0, HexBin.Count);
Debug.Log(rand1 + "rand1");
Debug.Log(rand2 + "rand2");
//Debug.Log(rand1);
// Debug.Log(rand1);
}
public void randomSpawn()
{
Instantiate(enemy, new Vector3(Random.Range(1, -2), 6, 1), Quaternion.identity);
randomizeHex();
}
// Update is called once per frame
void Update()
{
if (question.text.Contains(totalVar1Txt.text) && question.text.Contains(totalVar2Txt.text))
{
result.text = "OK";
}
switch (HexToBinary.rand1)
{
case 1:
valGroup1 = "1";
break;
case 2:
valGroup1 = "2";
break;
case 3:
valGroup1 = "3";
break;
case 4:
valGroup1 = "4";
break;
case 5:
valGroup1 = "5";
break;
case 6:
valGroup1 = "6";
break;
case 7:
valGroup1 = "7";
break;
case 8:
valGroup1 = "8";
break;
case 9:
valGroup1 = "9";
break;
case 10:
valGroup1 = "A";
break;
case 11:
valGroup1 = "B";
break;
case 12:
valGroup1 = "C";
break;
case 13:
valGroup1 = "D";
break;
case 14:
valGroup1 = "E";
break;
case 15:
valGroup1 = "F";
break;
}
switch (HexToBinary.rand2)
{
case 1:
valGroup2 = "1";
break;
case 2:
valGroup2 = "2";
break;
case 3:
valGroup2 = "3";
break;
case 4:
valGroup2 = "4";
break;
case 5:
valGroup2 = "5";
break;
case 6:
valGroup2 = "6";
break;
case 7:
valGroup2 = "7";
break;
case 8:
valGroup2 = "8";
break;
case 9:
valGroup2 = "9";
break;
case 10:
valGroup2 = "A";
break;
case 11:
valGroup2 = "B";
break;
case 12:
valGroup2 = "C";
break;
case 13:
valGroup2 = "D";
break;
case 14:
valGroup2 = "E";
break;
case 15:
valGroup2 = "F";
break;
}
question.text = valGroup1 + " " + valGroup2;
totalVar1Txt.text = boxesTouch.totalVarGrp1.ToString();
totalVar2Txt.text = boxesTouch.totalVarGrp2.ToString();
//question.text = HexToBinary.HexBin[rand2].ToString() +" "+ HexToBinary.HexBin[rand1].ToString();
//enemy.transform.Translate(0, speed * Time.deltaTime, 0);
}
//spawnExplosion();
}
回答:
你有这样的:
switch (HexToBinary.rand1)
{
case 1:
valGroup1 = "1";
break;
再后来在一些注释代码:
HexToBinary.HexBin[rand1].ToString()
你的问题是,数组(至少,大量o f语言,包括C#)start at index 0。
指数1在这里(你设置valGroup1
通过你的switch语句中的字符串"1"
)实际上是2
值:
HexBin.Add(1); // <-- index 0
HexBin.Add(2); // <-- index 1
当然......你错过值无论如何......这样就可以解决你的问题了。作为一个方面说明,根本不需要switch语句(少两次,你可以将它封装为一个函数!),你可以使用ToString("X")
来代替。这将真的让事情变得更容易!
我也看到一些变量被视为静态,有些没有(甚至静态的变量被HexToBinary.var
和var
混合引用),没有明确的原因。如果您需要从场景there are other ways中其他位置的其他脚本访问这些值。
以上是 从阵列列表中产生一个数字,显示前面的数字 的全部内容, 来源链接: utcz.com/qa/259019.html