- Home /
Change default sprite position when dragged into Hierarchy
I typically drag sprites into the hierarchy. A variety of gameObjects represent different rooms in my game. When I drag a sprite into the hierarchy, it always is set to global 0,0,0 position. What I want to do is write a small script that overrides this behavior so it is set to 0,0,0 in local position (so it can be centered on the room gameObject that I have dragged the sprite into). Is this possible?
If you create a prefab, you can specify predefined positions for objects to appear, but there is nothing that I know of to assign a gameobject position relative to the parent transform. You could do it in script for instantiation purposes in game, but I don't believe it can be done in the editor by default.
You may be able to write an editor extension that does it, although I couldn't advise you where to start looking on that.
Answer by CesarNascimento · Jan 19, 2016 at 09:00 PM
You could use a small script like this and attach it to your prefab.
using UnityEngine;
[ExecuteInEditMode]
public class ZeroPosition: MonoBehaviour
{
bool isInScene = false;
void Update ()
{
if (!isInScene) {
transform.localPosition = Vector3.zero;
isInScene = true;
}
}
}
Your answer
Follow this Question
Related Questions
Editor script to slice sprites 4 Answers
Add gameObject/transform to script component slot with editor 1 Answer
Instantiate a GameObject at a position specific to an element that was found in a text 1 Answer
Forcing all new GameObjects to a specific Position z-value 2 Answers
Object's sprite and collider are not at same position as transform 1 Answer