Is there another way to get a location value?

When I was first getting started with Unity, when I was creating certain effects like instantiating an object or setting points where I checked for overlapping objects, I was taught to create a child object and use that object’s transform for the position I needed.

This works, and it is a good way for teaching newcomers, but I am thinking this isn’t very efficient.
I have to create a whole game object, which is going to store a number of various attributes, when all I really need is a transform. Shoot, I don’t even need a whole transform; I just need the position. That’s a lot of data to add to my scene when all I needed was a vector3. A vector 2, even.

So I’m thinking about ways I can trim some of this fat.
The obvious answer is to just store a Vector3 or a Vector2 value in my scripts instead of a whole transform, and give that value to functions that need that position value.
But that isn’t very user-friendly. If I just type in a location in X and Y values, how am I certain that it is exactly where I want it to be? Using a gameobject’s transform let me see an actual point in space that I could move around and line it up exactly where it needs to be. If I were, say, designating where a monster spits fireballs from, I can grab the arrow gimble and line it up exactly with where the mouth appears on the sprite. But if I was doing that with just numbers, well it’s a little harder to get it lined up exactly where I want it.

So I’m wondering if there is another method I missed. Something that combines the ease of using another game object, but without all that overhead. Perhaps there is some way to produce a position and parent it to an object, but only possessing a position? Or only possessing a transform?

I’m not entirely sure that I understand what you’re saying, so if I’m dead wrong, sorry about that. But…

When you want to have something in the game world (scene), you need to have at least one game object.
Every game object automatically have a transform.
When you write a script reading or manipulating positions, you can reference the transform. It is the easiest way.
The reference to the transform is just a reference, it is not heavier than having a Vector3 position in terms of memory.
So don’t worry about this, it’s a non-issue.

I mean if I have a position value other than the object’s transform.
So like if I have a creature that spits fireballs, I need a position to dictate where the fireballs are spawned from.
What I was originally taught to do was to create a second game object, make it a child of the creature, and then I can use a reference to that second transform as the position to spawn the fireball from.

There is added overhead because I have a second game object with all of its data, when I only needed the position value from the transform.

I’m wondering if there is another way to handle that.
I could just have a vector2 or vector3 value, but then I can’t use a handle to see exactly where it is relative to the object. So I’m looking for a happy middle-ground.

I worry this is a premature optimization. An empty (transform-only) GameObject is not expensive. It has no per-frame performance penalty. It will slow down GameObject.Find calls a little bit, but those are bad ideas anyway.

Are you having performance or memory issues? Have you tracked them to be caused by a lot of empty GameObjects? If not I wouldn’t worry about it. Empty GameObjects are by far the most user friendly way of specifying world positions, as you noted.

1 Like

So I’ve dealt with this before and had two solutions.

The first was a small tool, which saved the positions of an objects children to a scriptable object in the editor.
So I could position the child objects, then save their positions to the SO then delete the child objects.
Allowing me to have collection of just positions.

The second was to write an editor script to add a context menu to the Scene view, where I could just right click in the position I want, and have it assign that position to an object.

My intention wasn’t optimization but more related to the dynamic nature of my game.
But I think one of those could work just the same for you.