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 funkastle · Mar 16, 2017 at 06:37 AM · referenceif-statementsfindreferencingfind-gameobject

How to reference a GameObject that shares position with gameObject

Hey there,

Well, thats a weird title. What i am trying to do is identify the object which position matches the gameObjects position that the script is attached to. Hell, that didn't make it better.

Here's my script:

  public Vector2 tilePosition;
  public float tileYPiece;
  
  void Update()
      {
          tilePosition = transform.position;
          tileYPiece = tilePosition.y + 0.5f;
  
          pieceIdentifier();
      }
  
  void pieceIdentifier()
      {
          if ((tileYPiece) == GameObject.FindGameObjectWithTag("Piece").GetComponent<PieceScript>().piecePosition.y && tilePosition.x == GameObject.FindGameObjectWithTag("Piece").GetComponent<PieceScript>().piecePosition.x)
          {
              //the GameObject I need to reference does something
            
          }
  
      }
  

So the tile position is a Vector2(x, y), and the piece's position is a Vector2 (x, y) as well, and I need to reference the GameObject that shares position with the tile.

I'm confused as hell, if you hadn't noticed, and would reeally like some help

Thanks in advance

Comment
Add comment · Show 4
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 hexagonius · Mar 16, 2017 at 06:45 AM 0
Share

is the script on a different gameobject? thorn you must have one for each gameobject you want to reference, right? why not making it a child gameobject, then it's just it.

avatar image tanoshimi · Mar 16, 2017 at 07:33 AM 0
Share

One of the first rules about dealing with floating point maths is that you should never directly compare the equality of two floats because, say, 1.0f + 1.0f is not necessarily exactly 2.0f. The fact that you are talking about "tiles" makes it sound like your objects can only occupy discrete positions that could be represented on a 2d grid - is that right?

avatar image ShadyProductions · Mar 16, 2017 at 01:49 PM 0
Share

You could retrieve the first gameobject which has it's transform's position the same as your piece ofcourse excluding your piece. Something like the following:

 GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
 
 var obj = allObjects.FirstOrDefault(c => c.transform.position.x == Piece.X && c.transform.position.y == Piece.Y && c != Piece);
 if (obj == null) throw new Exception("No object with same position found!");

Also note that comparing float's isn't going to always give you the wished for results.

avatar image TreyH · Mar 16, 2017 at 03:56 PM 0
Share

Probably not your issue, but direct float equality comparisons are not always intuitive. It can often be helpful to compare their difference to some negligible value, rather than seeing if they values are "equal."

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.find and Instantiate do not return the same type? 1 Answer

Loading a specific set of sprites with addressables 0 Answers

Duplicating Component with Some Scene References in Serialized Scriptable Object Class Reference 0 Answers

store a reference to a float in a variable? (similar to what the "ref" keyword does) 2 Answers

Drag and drop reference not working? 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