Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Enduriel · May 08, 2019 at 07:06 PM · unity 5physicscollideroverlapsphere

Physics.OverlapSphere not detecting Instantiated Objects

I'm trying to make a grid (in the future hopefully also other shapes) of Hexes in which all the Hexes find their neighbors by creating a Physics.OverlapSphere and adding all Gameobjects that are within a certain radius (in my case 2 as the radius of the circumcircle of the Hexes is sqrt(3). I instantiate all the Hexes inside of a Hexgrid GameObject

private void GenerateMap()

 {
     for (int x = 0; x < size; x++)
     {
         Vector3 pos = gameObject.transform.position;
         if (x % 2 != 0)
             pos.z -= 1.5f;
         pos.x = gameObject.transform.position.x + 2.25f * x;

         for (int i = (x % 2); i < size * 2; i += 2)
         {
             GameObject newHex = Instantiate(hexPrefab);
             pos.z -= 3;
             newHex.transform.position = pos;
             newHex.name = x + "_" + i;
             pos = newHex.transform.position;
             board[x, i] = newHex;
             hexes.Add(newHex.GetComponent<Hex>());
             newHex.transform.parent = transform;
         }
     }
 }

and then call a GiveNeighbors function to find all the neighbors of a Hex

private void GiveNeighbors(GameObject hexObject)

 {
     Collider[] colliderNeighbors = Physics.OverlapSphere(hexObject.transform.position, 2);
     //hexObject.GetComponent<Hex>().makeCircle(2);
     //print(colliderNeighbors.Length + " | " + hexObject.name + " | " + 
     hexObject.GetComponent<Collider>().transform.position);

     foreach (Collider collider in colliderNeighbors)
     {
         if (collider.gameObject.GetComponents<Hex>() != null && collider.gameObject != hexObject)
         {
             hexObject.GetComponent<Hex>().neighbors.Add(collider.gameObject.GetComponent<Hex>());
         }
     }
 }

hexPrefab: alt text However, Physics.OverlapSphere does return the colliders of any instantiated objects (including a Pawn object that should still be recognized) but it does return the colliders of objects that were placed on the scene before I run the game: alt text The List of neighbors for that Hex contains ONLY the hex that I placed there beforehand that you can see hovering slightly above.

I have no idea how or why this has been happening and after about 4 hours of scouring the internet I've given up and turned to here for any help.

(I know I could do this differently, but using a Physics.OverlapSphere lets me be very flexible the future shape of my map which is why I'm really trying to make it work like this)

Any help would be VERY much appreciated.

upload-2019-5-8-13-18-10.png (270.3 kB)
upload-2019-5-8-13-13-24.png (197.3 kB)
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

Answer by imaginationrabbit · May 08, 2019 at 07:53 PM

I would look at when the OverlapSphere is running- it might be running before the objects are instantiated so it would only return the objects already in the scene.

Comment
Add comment · Show 1 · 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 Enduriel · May 08, 2019 at 09:38 PM 0
Share

@mdotstrange The OverlapSphere is created based on the position of the GameObject, and since the position is correct (only the hexes that should detect the NonInstantiated Hex do so) the Objects must already be instantiated, also, you can see the order here:

 void Awake()
 {
     if (instance == null)
     {
         instance = this;
     } else
     {
         Destroy(gameObject);
     }
     Pawn.LifebarPrefab = LifebarPrefab;
     DontDestroyOnLoad(gameObject);
     Test();
     hexGrid.GetComponent<HexGridNew>().$$anonymous$$akeBoard(20);
     hexGrid.GetComponent<HexGridNew>().SpawnPawns(aliens);
     hexGrid.GetComponent<HexGridNew>().SpawnPawns(nazis);
     hexGrid.GetComponent<HexGridNew>().$$anonymous$$akeLinks();
 }

Where $$anonymous$$akeBoard() is run first

 public void $$anonymous$$akeBoard(int size)
 
 {
     this.size = size;
     board = new GameObject[size, size * 2];
     Generate$$anonymous$$ap();
 }

Instantiating all the Hexes, and only then $$anonymous$$akeLinks()

 public void $$anonymous$$akeLinks()
 {
     for (int x = 0; x < board.GetLength(0); x++)
     {
         for (int y = 0; y < board.GetLength(1); y++)
         {
             if (board[x,y] != null)
             {
                 GiveNeighbors(board[x, y]);
             }
         }
     }
 }

Thank you for your answer though... I'm sorry if I sound frustrated but I really am.

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

273 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random results with physics.OverlapSphere and AOE attacks 0 Answers

How to build a spaceship with interior? 1 Answer

Problems with raycast accuracy 1 Answer

Detect collider collision ,identify and access colliding colliders 0 Answers

Excluding Vertices from Cloth Colliders in Unity 5 4 Answers


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