How to fix a game objects that changes its position when viewed in game mode?

I have a sentry gun scripted to follow or look at my character. In the scene view the sentry gun rests flat on a surface, but in game view it is turned on its side… Why does this happen and how can I fix it?

The sentry gun is a 3D game object with no rigid body, has mesh renderer, mesh collider, textures and a script to look at target. It will be a simple, stationary AI/enemy character when finished.

Script:

[8964-screenshot+(1).png|8964]

Imagine the sentry gun or game object is attached to a DVD that lays flat on the ground when in scene view, but when you go into game view, that DVD and game object are standing on its side.

LookAt will rotate the object to make it’s forward axis (the blue z axis) point towards the target and the up vector (the green y axis) will point upwards. Your object’s forward and upward axis must match these or the gun will of course pointing somewhere else.

To fix this, either rotate your gun in your modelling application so it comes in the right way, or use an empty gameobject which will be your actual gun (so attach your script there) and add your model as child. You can rotate the child so it’s forward axis will match the parents forwards direction.