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 JVILL · Jan 30, 2013 at 11:56 AM · terrainpositionobjectdestroyget

Remove object based on its position alone

Language = Javascript

I have a number of terrain pieces tiled together.

At some point I want the Destroy them with some script.

But the terrain objects themselves are all clones.

All I need is something that allows me to grab / identify the object at an X and Z position that I already know.

ie:

Destroy ( get.gameObject(x,y,z) ) ;

I realise this code above doesn't work... but it gets my point across. Can anyone help me with this? I've heard about the Sphere overlap thing but it might be more complicated then I can handle. Is there any simple command?

Perhaps I could name the terrain object with their locations in their name instead and destroy them that way?

Comment
Add comment · Show 5
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 Berenger · Jan 30, 2013 at 12:25 PM 0
Share

You can use brute force and get the list of every game objects : GameObject.FindObjectsOfType(typeof(GameObject)). Then, you go through them all, check the position, destroy if need be.

Or you can narrow that list down with Physics.OverlapSphere, if they have a collider.

avatar image save · Jan 30, 2013 at 12:34 PM 0
Share

If you know the objects that is to be destroyed from start then the best solution is to cache them when you create them. Example:

     //Create
     var tileObjects : GameObject[] = new GameObject[amountOfTiles];
     for (var i=0; i<amountOfTiles; i++)
         Instantiate(tilePrefab, Vector3(0,i,0), Quaternion.identity);
 
     //Destroy
     for (var tile : GameObject in tileObjects)
         Destroy(tile);

If you want to do it for only certain objects within a position then a simple if-statement would do before you destroy the object.

avatar image JVILL · Jan 30, 2013 at 12:44 PM 0
Share

They have a Terrain Collider. It is to be designed as an infinite tile-based terrain, so BruteForce is not wise. I only want the 1 64x64 terrain piece at say position (0,0,0) to be destroyed. Because the tile piece is random, I do not know what piece is placed.

avatar image Berenger · Jan 30, 2013 at 01:16 PM 0
Share

$$anonymous$$oved to comments. It's not in the right order anymore though, sorry about that.

avatar image robertbu · Jan 30, 2013 at 05:49 PM 0
Share

You could call Component.Broadcast$$anonymous$$essage() with a position as a parameter. Each Terrain piece could decide if it is close enough to the position to be destroyed and then destroy itself.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeffom · Jan 30, 2013 at 01:40 PM

try to make a dictionary to map the position of your objects (or create an xml) If you create your tiles on the fly do something like this

 Dictionary< Vector3, Gameobject > myMap;
 void CreateTile()
 {
     Gameobject myTileObj;
     //blah blah create and place tile and set it to the myTileObj
     //Supposing that 2 tiles will never have the same position then
     if( !myMap.Contains( myTileObj.transfor.position ) )
         myMap.add(Vector3, myTileObj);        
 }

 Vector3 GetTileByPosition( Vector3 position )
 {
      if( myMap.Contains(position) )
          return myMap[position];
 }

tell us later if this works or not

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 JVILL · Jan 30, 2013 at 08:53 PM 0
Share

I'll try this later today. Although I will mention that the tile data is already stored in a two-dimensional Array. The Array is in another script (and I'm not sure how I access it).

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

12 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

Related Questions

How do I set and get the position of a object? 3 Answers

C#: Changing current position of an object in y. 1 Answer

Object won't instantiate at parents position(solved) 2 Answers

Destroy and instantiate far objects 2 Answers

Random object placement. 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