Move spawn attacker higher

Hey guys, I am trying move a bit high my attackers because now on line 4 are under line.

Original function:

    void Spawn (GameObject myGameObject){  //myObject = thisAttacker
    GameObject myAttacker = Instantiate(myGameObject) as GameObject;  //vytvorim mojho attackera
    myAttacker.transform.parent = transform;    //dam to do rodica
    myAttacker.transform.position = transform.position; //neotvori mi objekt,priradi ho vnorene
}

I am trying something like: (but its doesnt work)

    void Spawn (GameObject myGameObject){ 
       GameObject myAttacker = Instantiate(myGameObject,new Vector3(0f,0.5f,0f),Quaternion.Identity) as GameObject; 
       myAttacker.transform.parent = transform;   
       myAttacker.transform.position = transform.position;

}

Hi Adrian. I don’t think what you are trying to do is best done in the code, you should probably simply move the position of the attacker’s sprite in the scene view window.

But if you want to know what goes wrong with this code, it is that you are instantiating your attacker game object at coordinates (0, 0.5, 0) in world space, then two lines below that you are setting its position to the parent’s position. That is why it does exactly the same thing as the original function.

If you really want to move the attacker 0.5 units above the parent position in code, you’d need to do something like this:

myAttacker.transform.position = transform.position + Vector3.up * 0.5f;