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
0
Question by Brawl · Feb 11, 2012 at 06:49 AM · instantiatepositioninstancegetid

Getting instance ID from position

Hello people! I'd like to start off by saying, yes, I am new to Unity, but I do have some game creating experience.

So, to the point. I'm trying to figure out how I can fetch the ID of an instance if all I know about it is it's position. I'm using this to destroy blocks (not a Minecraft clone.) Basically, I'm looking for something that will return the ID of an object at whatever location I chose.

Not really sure where to start on this honestly. Oh! One more bit of information that may or may not be helpful. All of these block instances where created using Instantiate. I created a prefab and placed them in the room in game using Instantiate.

Also, I'm trying to stick to Javascript for the time being. Learning two new languages at once would be a bit of a headache, but if C# is the way to go on this, then so be it. :)

I believe that is all of the information that I have to offer at this moment.

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
1
Best Answer

Answer by syclamoth · Feb 11, 2012 at 06:59 AM

Short answer: no, you can't get an object reference just from position data alone, without creating a complex object indexing system (look up octrees, it's where I'd start on this one).

However, for what you are doing here, there's probably a simpler way. If the objects you are trying to find have colliders, you can use Physics.Raycast or Physics.Linecast to get information about colliders detected in a line. If you know two points that the object is definitely between, you can use one of these functions to give you a reference to the first object found that intersects that line.

If your blocks are arranged in a grid, you can also use a more straightforward kind of indexing system. If you create a finite grid, with a 'size' in each dimension and a scale, you can transform world-coordinates to grid-coordinates fairly simply, and then if your blocks are stored in a 2d (or 3d) array that corresponds to the grid, you can access them like that (by transforming world coordinates to grid coordinates, and then indexing the array based on the grid coordinates).

If your objects are guaranteed to remain in one spot, you could also use a dictionary, that maps between Vector3 positions, and Transform objects. That way, you can find the object that is exactly in the point that you specified. I wouldn't recommend this, however, because Vector3s may not be precise enough to perfectly map in the way that you would need for this. (Also, I'm not especially clear on how to use .net generics in UnityScript- I'm told it's possible, but I've never done it.)

Comment
Add comment · 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

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

when testing in the game mode , shots don't fire ? 1 Answer

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

Instance child global position 1 Answer

How to set the mouse x and/or z position as a variable 1 Answer

How do I get the global position 2 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