Attach a prefab to a object? c#

I have been trying to make a script for some hours, there instantiate a prefab, and then gets the prefab to follow the object, but i hasn’t successes yet.
I have tried many tings, like in update i give it the same coordinates as the object there is moving. And the rotation fucks too :frowning:
I recently just started scripting, so im still learning, please help.

my script is:

public GameObject Prefab;
public GameObject MyShip;

void Update()    
{
if (Input.GetKeyDown(KeyCode.B))
        {
            Instantiate(Prefab, MyShip.transform.position, MyShip.transform.rotation);
        }
}

my game is where im a spaceship, and i need to add a gun to the ship. so can you help me making my prefab/gun attached on my object/ship, and have the same rotation (:

You must child the created gun to the ship:

void Update()    
{
if (Input.GetKeyDown(KeyCode.B))
        {
            // get a reference to the newly created object:
            GameObject obj;
            obj = (GameObject) Instantiate(Prefab, MyShip.transform.position, MyShip.transform.rotation);
            // child the object to MyShip:
            obj.transform.parent = MyShip.transform; 
        }
}

The object will follow the ship in position and rotation, but you can rotate/move it if you want.