-1

I need to spawn a gameobject in Unity, where it will generate numbers.

For some,reason the prefab is generating the numbers earlier than supposed to. For example: The prefab displays "10", but the real variable is actually 24. After the "10" prefab gets destroyed, another one spawns with "24". So basically, the prefab is being 'delayed'.

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();
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
hurkaperpa
  • 131
  • 11
  • use the debugger and step thru the code .. it's obvious that you are doing something incorrectly. – MethodMan Dec 03 '17 at 00:50
  • 1
    Not being rude but any question should be of general interest for everyone. Not simply a request to debug your specific code. – RCYR Dec 03 '17 at 01:45

1 Answers1

1

You have this:

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

Then later in some commented code:

HexToBinary.HexBin[rand1].ToString()

The problem you have is that arrays (at least, in a large number of languages, including C#) start at index 0.

Index 1 here (which you set valGroup1 to the string "1" via your switch statement) is actually a value of 2:

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

Of course...you're missing the value-zero anyway...so that'd fix your problem too.

As a side note, you don't need the switch statement at all (much less twice, you can encapsulate it as a function!), you can just use ToString("X") instead. That'll really make things easier!

I also see some variables being treated as static and some not (and even the static ones are referenced with a mix of HexToBinary.var and just var), with no clear reason why. If you need to access these values from another script attached somewhere else in your scene there are other ways.