从一个arraylist中产生一个数字,显示一个早期的数字

时间:2017-12-03 00:42:09

标签: c# unity3d

我需要在Unity中生成一个游戏对象,它会生成数字。

对某些人来说,原因是预制件产生的数字早于预期。例如:预制件显示" 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();
}

1 个答案:

答案 0 :(得分:1)

你有这个:

   switch (HexToBinary.rand1)
   {
       case 1:
           valGroup1 = "1";
           break;

稍后在一些评论代码中:

HexToBinary.HexBin[rand1].ToString()

你遇到的问题是数组(至少在很多语言中,包括C#)start at index 0

此处的索引1(通过switch语句将valGroup1设置为字符串"1")实际上是2的值:

   HexBin.Add(1); // <-- index 0
   HexBin.Add(2); // <-- index 1

当然......无论如何,你都错过了零价值......这样也可以解决你的问题。

作为旁注,您根本不需要switch语句(更不用说两次,您可以将其封装为函数!),您只需使用ToString("X")即可。

我还看到一些变量被视为静态而一些变量没有(甚至静态变量都被HexToBinary.varvar混合引用),没有明确的原因。如果您需要从场景there are other ways中其他位置附加的其他脚本访问这些值。