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
1
Question by DasNanda · Sep 29, 2017 at 07:52 AM · gameobjecteditoreditor-scriptingeditor extension

Finding a GameObject in an editor script based on position

So I'm trying to write my own Level Editor. I already experimented with it and there's one issue I just can't get around. I want to have a simple eraser tool that deletes objects from the scene when I click on them. My problem is that I can't figure out a way to detect these objects. I could use raycasting, but I really don't want to have a collider attached to every game object I place. One way I did it before, was to go through every object in the scene and delete the one with the right coordinates. The problem with that is that for one I'm afraid this will cost a lot of performance with bigger levels. Also, my goal is to create a Grid with adjustable size. That means there could be objects placed beside each other and one has an X coordinate of 0.5 while the other is on 1. So with my old approach, I'm only able to delete blocks placed in the same grid size as I have selected at the moment.

I hope it's clear what I'm trying to achieve. 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

2 Replies

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

Answer by MaxGuernseyIII · Sep 29, 2017 at 08:17 AM

Can you just attach a component that traps OnMouseup? Something like this...

 using UnityEngine;
 using UnityEngine.Events;
 
 public class Clickable : MonoBehaviour
 {
   public UnityEvent Clicked;
 
   void OnMouseUp()
   {
     Clicked.Invoke();
   }
 }

You can add one of those into each game object created by your editor and attach whatever handlers you like to the click event. You can also use something like this:

 using UnityEngine;
 using UnityEngine.Events;
 
 public class Clickable : MonoBehaviour
 {
   public UnityEvent<GameObject> Clicked;
 
   void OnMouseUp()
   {
     Clicked.Invoke(gameObject);
   }
 }

That doesn't seem to work as well with the inspector but it might make programmatic interception of a click easier.

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 DasNanda · Sep 29, 2017 at 10:35 AM 0
Share

Thank you for the answer! This looks really promising, so I'll try. I'm not very advanced at coding though, so I don't fully understand what's going on here. I'm not asking you to explain it all to me, but if you don't $$anonymous$$d could you give me some kind of reference point on what to research?

avatar image MaxGuernseyIII DasNanda · Sep 29, 2017 at 06:16 PM 0
Share

Sure.

  • https://docs.unity3d.com/ScriptReference/GameObject.html -- the API for a game object, the "atom" of the Unity universe

  • https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.html -- the reference for the base class for behaviors, the "sub-atomic particle" of the Unity universe; the things you plug into a game object to make them work differently

  • https://docs.unity3d.com/ScriptReference/GameObject.AddComponent -- how you add a behavior to a game object in a script

  • https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseUp.html -- the event being intercepted by the behavior code I pasted

  • https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html -- a way of broadcasting events to other scripts; .NET events would work, too, from an API perspective but a UnityEvent gets an editor in the inspector

avatar image DasNanda MaxGuernseyIII · Sep 29, 2017 at 06:57 PM 1
Share

Thanks a lot! :)

avatar image
1

Answer by tormentoarmagedoom · Sep 29, 2017 at 08:08 AM

Good day @DasNanda.

As you say you discarted the options i can think to your problem. Instead of Raycasting, i should create a empty collider to see what objects collide with it to delete them.

The other option can be call a funtion in each object that cheks the position.

I think a solution is to use the colliders to detect the objects in the delete area, is the most efficient way to it. (Raycasting or OnTriggerStay). And yea, you need to create a Collider for each object

But @MaxGuernseyIII answer is also very good. You should try it!

Bye :D :D

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 DasNanda · Sep 29, 2017 at 08:15 AM 0
Share

Haha, thank you for the quick answer :D I guess I'm forced to do something like that. I'll make it so that the colliders will be deleted after the game starts. It's top down so there will be a lot of ground tiles taking up memory with their colliders, that's what I was worried about.

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

125 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

Related Questions

in-editor cloning of game objects 1 Answer

Is there any way to make a timeline feature in editor scripting? 1 Answer

Any way to lock an object from changes in the editor? 4 Answers

Automatically update prefab instance 1 Answer

How to add a component on a GameObject in Custom Inspector 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