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();
}