- Home /
Checking Array of GameObjects if it contains selected GameObject (C#)
For more immersion into getting what it is, I'm making a strategy game which is based on provinces, each province acts as GameObject which has "Province" Script, which contains info, such as NeighboringProvinces (This would be our array). And I'm also using script, which is called ProvinceControl, it's purpose is to check if selected province is a neighboring province, using the array that is given in Province script and set in inspector.
But I'm not sure how to check if selected province gameobject is in the list of current province array of gameobjects.
This is how my ProvinceControl script looks like.
using UnityEngine;
using System.Collections;
public class ProvinceControl : MonoBehaviour {
//currentProvince and currentUnit are selected in inspector
public GameObject currentUnit;
public GameObject currentProvince;
public bool isNeighboringProvince;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if( Input.GetMouseButtonDown(0) )
{
//We select province with Raycast
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null)
{
GameObject currentSelectedProvince = hit.collider.gameObject;
Debug.Log ("Province Name: " + hit.collider.gameObject.name);
//If it is neighboring province, sadly this will never happen for now since we can't find out if it is neighboring province
if(isNeighboringProvince)
{
currentUnit.transform.position = new Vector3(currentSelectedProvince.transform.position.x, currentSelectedProvince.transform.position.y, currentUnit.transform.position.z);
}
}
else
{
Debug.Log("Didn't hit anything");
}
}
}
}
And this is how my Province script looks like.
using UnityEngine;
using System.Collections;
public class Province : MonoBehaviour {
//Provines selected in inspector
public GameObject[] neighboringProvinces;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I'd like answer/example in C# please, and if there's more efficient way of doing this than I am doing right now, would be really good to know.
Thanks in advance!
Answer by fafase · Jun 15, 2014 at 09:01 PM
All in all you want to check if an array contains a specific item, have you thought of
bool result = Array.Contains(array, item);
Now there is different approach in defining what are neighbours, simple and annoying, you have your array in Inspector and you drag and drop each neighbour in the main one.
The second approach would require you to develop a basic script but I cannot really tell exactly because it would depend on the shape of your provinces. if all squares, then you can use a 2D array and consider the each province (x,y) has neighbour
(x-1, y)
(x+1, y)
(x, y+1)
(x, y-1)
I haven't thought of that way, but even if I would try to, how would I make my script access an array that is in other gameobject's script?
And about that different approach of using dragging and dropping in inspector, I've already made it like that, it's just that I don't know how to access information of each province which is located in different gameobject(Province), and script it has with itself.
You need to look into GetComponent. I would suggest to master that one before trying anything fancy.
I know the basics how it works, but just as how I try to do it makes compiler error, so I'd like to see an example, so I would at least would know what I am doing wrong.
I've changed my script a bit, this is how it looks like right now.
using UnityEngine; using System.Collections;
public class ProvinceControl : $$anonymous$$onoBehaviour {
//currentProvince and currentUnit are selected in inspector
public GameObject currentUnit;
public GameObject currentProvince;
public bool isNeighboringProvince;
Province provinceInfo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if( Input.Get$$anonymous$$ouseButtonDown(0) )
{
//We select province with Raycast
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null)
{
GameObject currentSelectedProvince = hit.collider.gameObject;
Debug.Log ("Province Name: " + hit.collider.gameObject.name);
provinceInfo = currentSelectedProvince.GetComponent<Province>();
isNeighboringProvince = provinceInfo.neighboringProvinces.Contains(neighboringProvinces, currentSelectedProvince);
//If it is neighboring province, sadly this will never happen for now since we can't find out if it is neighboring province
if(isNeighboringProvince)
{
currentUnit.transform.position = new Vector3(currentSelectedProvince.transform.position.x, currentSelectedProvince.transform.position.y, currentUnit.transform.position.z);
}
}
else
{
Debug.Log("Didn't hit anything");
}
}
}
}