- Home /
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.
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.
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?
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
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
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.