- Home /
Question by
LeeLime5000 · May 23 at 01:51 PM ·
unity 2d
Checking if value in array not working
My goal for my code is to create an array of sprite in game, but 10 of the sprites are bombs. In which currently it does not do that. Currently, my if statement (I don't think) is not check for all possible guesses to get all 10 bombs.
foreach (Vector3 b in BombCords)
{
if (n && Mathf.Round(b.x * 10.0f) * 0.1f == Mathf.Round(i * 10.0f) * 0.1f && Mathf.Round(b.y * 10.0f) * 0.1f == Mathf.Round(l * 10.0f) * 0.1f){
g = Instantiate(Bomb1, b, new Quaternion(0,0,0,0));
n = false;
Debug.Log("Bomb");
t = true;
break;
}
else if (n == false && Mathf.Round(b.x * 10.0f) *
0.1f == Mathf.Round(i * 10.0f) * 0.1f && Mathf.Round(b.y * 10.0f) * 0.1f == Mathf.Round(l * 10.0f) * 0.1f){
g = Instantiate(Bomb2, b, new Quaternion(0,0,0,0));
n = true;
Debug.Log("Bomb");
t = true;
break;
}
// Debug.Log(b.x + " = " + Mathf.Round(i * 10.0f) * 0.1 + ", " + Mathf.Round(b.y * 10.0f) * 0.1f
+ " = " + Mathf.Round(l * 10.0f) * 0.1);
}
Right now their is 8 or 9 bombs coming up, and rarely is 10. I know that it is not instantiating the entirety 10 bombs because my Debug.Log("Bomb")
only outputs the amount of bombs on the map.
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using TMPro;
using System.Collections;
using UnityEngine.EventSystems;
public class BlockStuff : MonoBehaviour
{
public GameObject Floor1;
public GameObject Floor2;
public GameObject Bomb1;
public GameObject Bomb2;
public GameObject Mined1;
public GameObject Mined2;
public Vector3[] BombCords = new Vector3[10];
void Update()
{
var mousePos = Input.mousePosition;
var mousePosWorld = Camera.main.ScreenToWorldPoint(mousePos);
RaycastHit2D raycast = Physics2D.Raycast(mousePosWorld, new Vector2(0,0), 1);
RaycastHit2D braycast = Physics2D.Raycast(mousePosWorld + Vector3.down, Vector2.zero, 0);
if (Input.GetMouseButtonDown(0)){
if (raycast.collider.gameObject.layer == 3){
GameObject g = Mined2;
if (BombCords.Contains(new Vector3(Mathf.RoundToInt(mousePosWorld.x-0.5f)+0.5f,Mathf.RoundToInt(mousePosWorld.y-0.5f)+0.5f,0 ))){
Debug.Log("BOMB");
}
if (raycast.collider.gameObject.tag == "Floor1"){
g = Instantiate(Mined1, raycast.collider.gameObject.transform.position, raycast.collider.gameObject.transform.rotation);
raycast.collider.gameObject.SetActive(false);
}
else{
g = Instantiate(Mined2, raycast.collider.gameObject.transform.position, raycast.collider.gameObject.transform.rotation);
raycast.collider.gameObject.SetActive(false);
}
g.name = "grass";
g.transform.parent = transform;
}
// raycast.collider.gameObject.transform == Mathf.RoundToInt(raycast.collider.gameObject.transform.position.x) ; //IN PROCESS OF CHANGING GRASS TO MINE FLOOR
}
}
void Start()
{
bool n = true;
bool t = false;
for (int i = 0; i < 10; i++){
var BombCord = new Vector3(Mathf.RoundToInt(Random.Range(-9.5f,0.5f)-0.5f)+0.5f,Mathf.RoundToInt(Random.Range(-4f,5f)-0.5f)+0.5f, -0.2f);
// for (int l = 0; l <= i; l++){
foreach (Vector3 b in BombCords)
{
while (true){
if (b == BombCord){
BombCord = new Vector3(Mathf.RoundToInt(Random.Range(-9.5f,0.5f)-0.5f)+0.5f,Mathf.RoundToInt(Random.Range(-4f,5f)-0.5f)+0.5f, -0.2f);
}
else{
break;
}
}
}
BombCords[i] = BombCord;
Debug.Log(BombCords[i]);
// Instantiate(Bomb1, BombCord, new Quaternion(0,0,0,0));
}
for(float l= -4.5f; l < 5.5; l+=1){
for(float i = -10.5f; i < 0.5; i+=1){
GameObject g = Floor1;
foreach (Vector3 b in BombCords)
{
if (n && Mathf.Round(b.x * 10.0f) * 0.1f == Mathf.Round(i * 10.0f) * 0.1f && Mathf.Round(b.y * 10.0f) * 0.1f == Mathf.Round(l * 10.0f) * 0.1f){
g = Instantiate(Bomb1, b, new Quaternion(0,0,0,0));
n = false;
Debug.Log("Bomb");
t = true;
break;
}
else if (n == false && Mathf.Round(b.x * 10.0f) *
0.1f == Mathf.Round(i * 10.0f) * 0.1f && Mathf.Round(b.y * 10.0f) * 0.1f == Mathf.Round(l * 10.0f) * 0.1f){
g = Instantiate(Bomb2, b, new Quaternion(0,0,0,0));
n = true;
Debug.Log("Bomb");
t = true;
break;
}
// Debug.Log(b.x + " = " + Mathf.Round(i * 10.0f) * 0.1 + ", " + Mathf.Round(b.y * 10.0f) * 0.1f
+ " = " + Mathf.Round(l * 10.0f) * 0.1);
}
if (n && t == false){
g = Instantiate(Floor1, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
n = false;
}
else if (n == false && t == false){
g = Instantiate(Floor2, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
n = true;
}
if (t){
g.name = "BOMB";
}
else{
g.name = "grass";
}
g.transform.parent = transform;
t = false;
// if (n && BombCords.Contains(new Vector3(i,l,-0.2f))){
// g = Instantiate(Bomb1, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
// n = false;
// Debug.Log("Bomb");
// }
// else if (n == false && BombCords.Contains(new Vector3(i,l,-0.2f))){
// g = Instantiate(Bomb2, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
// n = true;
// Debug.Log("Bomb");
// }
// else if (n && BombCords.Contains(new Vector3(i,l,-0.2f)) == false){
// g = Instantiate(Floor1, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
// Debug.Log(i + " " + l + " " + g.transform.position);
// n = false;
// }
// else if (n == false){
// g = Instantiate(Floor2, new Vector3(i,l,-0.2f), new Quaternion(0,0,0,0));
// n = true;
// }
}
}
}
}
im sorry if my title isnt right, if it is, please tell me some suggestions to change it to
,thanks
Comment