Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Zukas · Jun 15, 2014 at 08:54 PM · c#raycastarray

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

  1. (x-1, y)

  2. (x+1, y)

  3. (x, y+1)

  4. (x, y-1)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zukas · Jun 16, 2014 at 04:17 PM 0
Share

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.

avatar image fafase · Jun 16, 2014 at 04:22 PM 0
Share

You need to look into GetComponent. I would suggest to master that one before trying anything fancy.

avatar image Zukas · Jun 18, 2014 at 03:21 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

NullReferenceError, Tilemap Array with Transforms and Raycast (C# with Demo) 0 Answers

Raycast show ParticleSystem through an Array 0 Answers

Multiple Cars not working 1 Answer

Inspector Doesn't Update Array Elements When Updated In Array Constructor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges