- Home /
Problem when manually creating game object in scene
Hello, I encountered this weird problem with unity when manually creating GameObjects in game scene. I am working on a 2D game, and have been working on this project for quite a while. Recently, when I right click in the hierarchy and create a primitive game object, such as a quad or a sprite, it automatically sets its z-position to -10. This is really annoying because this disrupts the order of objects and I have to manually set it back to 0. However, when I drag an imported sprite into the scene, its z-position is not set to -10 automatically. I have no idea how to fix this problem, so I hope someone can point out the issue for me. Thank you.
Answer by henrimh · Nov 09, 2017 at 09:13 AM
I think it depends on what you have previously selected in the hierarchy, or where your camera is pointing in scene view (if viewing the scene in 3D). Try selecting different objects and then create stuff or move the scene view camera around.
$$anonymous$$y scene view is 2D, and I have not selected any objects in the hierarchy before I create a new game object. I have been having this problem for a couple of days and have been messing around with camera, scene view, etc. Apparently nothing solves the problem. However, this problem only happens in this particular project, so I guess I have accidentally changed something without noticing.
I figured it out. It is the camera positioned in the scene. I thought camera position in the scene does not matter in 2D view, but apparently it does. I focused back to zero position and everything works fine. Thank you for your answer.
Answer by Daemonhahn · Nov 09, 2017 at 12:35 PM
Unity does this by default as the default camera position is 0,0,0 so this would make the objects you place, viewable.
If you would like to change this, write an editor script that adds your own create functions to the list in the create menu. This is very easy to do:
https://docs.unity3d.com/ScriptReference/MenuItem.html
would be needed to make the class accessible from the menu,
after that just have it instantiate the object, and place it at 000 or wherever you want.
And there you have it, you'll have a custom hierarchy pipeline!
Sorry, Daemonhahn, I did not mark you answer as the best one, but I still appreciate you help very much. You did provide a very good alternative solution for the problem. Thank you again.
Answer by Guwang · Nov 09, 2017 at 12:47 PM
First of all, thank you for your help. I understand that I can use a custom editor to get rid of the problem, but even when I create a new camera in scene, its z-position is set to -10 automatically. On top of that, I have not had this problem before in the same project. This problem showed up just a couple days ago. In the past, I was able to create an primitive or empty game object or camera that would not go to -10 z automatically.
Right but once you've made the new camera, use your custom editor to set its position to 000 like I said?
That's why you use the editor, so that after adding an item you can override its positon and position it where you like.
I am not so familiar with unity editor, so I only know how to create a custom game object using unity editor, but how can I reset the transform of a primitive using editor?
What?
I am confused.
The same way you would for anything?
transform.positon = new Vector3(0,0,0);
Do that after instantiating it.
I'm not sure you understand, let me elaborate:
$$anonymous$$ake a custom editor script that has a menu item attribute at the top called something like "Assets>Create>centredCamera" so you can access it from a sensible menu.
Inside that menu item method, have it instantiate a camera and then set its position to 0 0 0.
If you struggle with editor stuff, look at https://docs.unity3d.com/ScriptReference/ScriptableWizard.html for an easy to make editor.