- Home /
How to find Gameobject with given coordinates
Hello I have been trying to find 3 objects in same column and blow them
with bad coding I'm stuck and I'm out of options how can I solve this problem what I did so far: Autogenerate hexatiles and placing select 3 of them end turn them
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Selector : MonoBehaviour
{
//public GameObject selector;
GameObject arac, arac2, arac3, hafiza, hafiza1,hafiza2, hafiza3;
public static Selector first,second,third;
Auto_Generator generator;
Vector2 firstPressPos, secondPressPos, currentSwipe;
string rotation;
Vector2 targrtloc;
// Start is called before the first frame update
void Start()
{
arac = GameObject.FindGameObjectWithTag("secici");
arac2 = GameObject.FindGameObjectWithTag("secici2");
arac3 = GameObject.FindGameObjectWithTag("secici3");
hafiza = GameObject.FindGameObjectWithTag("ghost");
hafiza.transform.position = new Vector2(-1.0f, -1.0f);
arac.transform.position = new Vector2(-1.0f, -1.0f);
arac2.transform.position = new Vector2(-1.0f, -1.0f);
arac3.transform.position = new Vector2(-1.0f, -1.0f);
first = null;
second = null;
third = null;
generator = GameObject.Find("Generator").GetComponent<Auto_Generator>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
if (Input.GetMouseButtonUp(0))
{
secondPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
currentSwipe.Normalize();
if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
{
checker(sonuclar.left);
dondur(sonuclar.left);
}
//swipe right
if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
{
checker(sonuclar.right);
dondur(sonuclar.right);
}
}
}
private void OnMouseDown()
{
hafiza.transform.position = transform.position;
Debug.Log(hafiza.transform.position);
if (first == null)
{
arac.transform.position = transform.position;
hafiza.transform.position = transform.position;
first = this;
}
if (first != null && second == null && hafiza.transform.position != arac.transform.position)
{
var f1 = Mathf.Abs(arac.transform.position.y - hafiza.transform.position.y);
var f2 = Mathf.Abs(arac.transform.position.x - hafiza.transform.position.x);
if (f1 == 1f || f1 == 0.5f && f2 >= 0 && f2 <= 1)
{
arac2.transform.position = hafiza.transform.position;
second = this;
first.targrtloc = second.transform.position;
}
}
else if (second != null && arac.transform.position != hafiza.transform.position && arac2.transform.position != hafiza.transform.position)
{
var f3 = Mathf.Abs(arac2.transform.position.y - hafiza.transform.position.y);
var f4 = Mathf.Abs(arac2.transform.position.x - hafiza.transform.position.x);
if (f3 == 1f || f3 == 0.5f && f4 >= 0 && f4 <= 1)
{
arac3.transform.position = transform.position;
third = this;
second.targrtloc = third.transform.position;
third.targrtloc = first.transform.position;
}
}
}
public void debug()
{
ress(sonuclar.debug);
}
void ress(sonuclar sonuclar)
{
if (sonuclar == sonuclar.debug)
{
first = null;
second = null;
third = null;
arac.transform.position = new Vector2(-1, -1);
arac2.transform.position = new Vector2(-1, -1);
arac3.transform.position = new Vector2(-1, -1);
}
else
{
//first = null;
//second = null;
//third = null;
}
}
void dondur (sonuclar sonuclar)
{
if (first != null && second != null && third != null )
{
if (sonuclar==sonuclar.right)
{
first.transform.position = Vector2.Lerp(arac3.transform.position, arac3.transform.position, 0.00001f);
second.transform.position = Vector2.Lerp(arac.transform.position, arac.transform.position, 0.00001f);
third.transform.position = Vector2.Lerp(arac2.transform.position, arac2.transform.position, 0.00001f);
ress(sonuclar.normal);
}
else if (sonuclar == sonuclar.left)
{
first.transform.position = Vector2.Lerp(arac2.transform.position, arac2.transform.position, 0.00001f);
second.transform.position = Vector2.Lerp(arac3.transform.position, arac3.transform.position, 0.00001f);
third.transform.position = Vector2.Lerp(arac.transform.position, arac.transform.position, 0.00001f);
ress(sonuclar.normal);
}
}
}
//GameObject.Destroy(gameObject);
//generator.regenerate(transform.position.x,transform.position.y);
void checker(sonuclar sonuclar)
{
if(sonuclar==sonuclar.left)
{
}
}
public enum sonuclar
{
debug,
normal,
left,
right
}
}
/Generator Codes/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Auto_Generator : MonoBehaviour
{
public GameObject[] objeler;
GameObject game;
public float width, height;
// float Xoffset= 0.619f;
float Yoffset = 0.5f;
public static Auto_Generator first, second, third;
int randomInt;
void Start()
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
randomInt = (Random.Range(0, objeler.Length));
game = Instantiate(objeler[randomInt]);
if (x % 2 == 0)
{
game.transform.position = new Vector2(x, y+0.5f);
}
else
{
game.transform.position = new Vector2(x, y );
}
}
}
}
public void regenerate(float x, float y)
{
Debug.Log("Silinen "+x+" "+y);
game = Instantiate(objeler[randomInt]);
game.transform.position = new Vector2(x, y);
}
}
Answer by ryanmillerca · Jul 19, 2019 at 09:38 PM
Ideally, you would manage your own 2 dimensional array to directly access these gameobjects from. In lieu of that, you can iterate over the objects, check their position, and return the closest match. First off, keep the spawned items in a list, so that we can iterate over them without including other items.
// declare the list in your Auto_Generator class
List<GameObject> spawnedItems = new List<GameObject>();
// then when you instantiate inside the Start() method, instantiate and add to list
game = Instantiate(objeler[randomInt]);
spawnedItems.Add(game);
Now you should have a list of your spawned items. You can create a method that returns matches by iterating through the list.
public GameObject GetItemAtCoordinate(int x, int y)
{
foreach (GameObject g in spawnedItems)
{
if (Mathf.RoundToInt(g.transform.localPosition.x) == x && Mathf.RoundToInt(g.transform.localPosition.y) == y) // you may use X and Z instead of X and Y, I am not sure
{
return g; // match found
}
return null; // no match found
}
That's nice but GetItemAtCoordinate() throwing expection (I'm dumb right now)
Error CS0161 'Selector.GetItemAtCoordinate(int, int)': not all code paths return a value
$$anonymous$$y bad. Add another return null;
after the foreach loop closes, and make sure to handle any null returns when you use the method, eg:
GameObject itemAtCoordinate = GetItemAtCoordinate(10,20);
if (itemAtCoordinate != null)
{
// do something
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Drawing a Mandelbrot Set problem 0 Answers
How to make if limit is reached then make sliders only slide back in ui (not forward) 2 Answers
Dropdown resolutions menu 0 Answers
Backtracking algorithm with conditions. 0 Answers