overriding while Instantiate

I am currently trying to place a prefab into the world with Instantiate(), but when I try to give it the command

Instantiate(prefab,transform.position,transform.rotation);

which should instantiate the given prefab at the current position, and orientation of the object holding the script. though when I run this script the object is instantiated with the position, and orientation defined by the prefab, and not with the position, and orientation of the object creating it.

I know that this line should work as I have used it for spawn points in my scenes, but it seems to be ignoring the arguments given to it.

EDIT steps taken

  • gave my playerControl script a public GameObject, and logic that when space bar is pressed to instanciate the GameObject
  • imported an fbx model (made if blender, cylinder with small alterations to orientation, and vetexes)
  • in a scene created an instance of the model at the origin (0,0,0)
  • I gave the instance a RigidBody, boxCollider, and a bulletScript (controls lifespan, and motion) this has been given to other GameObjects, and functions properly
  • created a prefab(giving it a unique name), and placed the instance inside
  • in the scene I associated the public GameObject with the prefab (check that the script variable has the right Prefab name)
  • then hit play

while in game whenever I press space bar the bullets will only spawn at the origin with the default orientation.

I have attempted the same actions with a Unity Cube (except for importing), and this would instanciate with the info from the player transform

I used this same logic when creating spawn points for my game (even for the player, and camera) which function as expected

Edit: just followed the same steps on a clean project (created all prefabs, and associated with the proper prefab for the projectile.

scipts in question are as follows:

// instanciated object script working as expected
public class BulletScript : MonoBehaviour {
    public float speed = 10;
    public float damage = 10;
    public float lifeSpan = 2;
    private float spawnTime;
    // Use this for initialization
    void Awake () {spawnTime = Time.time;}

    // Update is called once per frame
    void Update () {
        transform.position += transform.forward*speed*Time.deltaTime;
        if(Time.time>=(spawnTime + lifeSpan)){
            Destroy(gameObject);
        }
    }

    void OnCollisionEnter(Collision collision){Destroy(gameObject);}
}

associated code for the player controller

public class PlayerController : MonoBehaviour {
    public GameObject firedWeapon;
    public float shotDelay = .5f;
    private float shotAt = 0;
    // snip
    void Update(){
        // snip
        if(((Time.time-shotAt) > shotDelay)&&(Input.GetAxis("Fire1")>0)){
            shotAt = Time.time;
            // seams like arg 2, 3 are ignored, but if I place the model into an empty gameObject then they are used.
            Instantiate(firedWeapon,transform.position,transform.rotation); 
        }
        // snip
    }
    // snip
}

code used for Player Spawn point (fully functional)

public class PlayerSpawnPoint : MonoBehaviour {
    public GameObject Player;
    void Awake () {
        Instantiate(Player, transform.position, transform.rotation);
    }
}

if I go in and modify the default position of the prefab the spawning bullet takes on that property which is what would be expected if only the object was given as an argument to instantiate.

Your script is correct and it works fine. I just tested it on an empty project, like this:

public GameObject prefab;

void Start () {
    Instantiate(prefab, transform.position, transform.rotation);
}

Then attached it to the Main Camera and moved the prefab position elsewhere. Its position was the camera’s.

So, from the top of my head, I can see at least 3 things that could be “wrong” around it:

  1. The prefab isn’t the proper one, and maybe it’s a container with a dislocated child
  2. The object to which the script is attached (which could be more than one) contains the unexpected transform
  3. Either object is moving too fast and you’re not realizing its been instantiated in the proper place but moving to a new place

You’ll have to provide more code or analyze your script on your own.

It’s a very the common problem. Your model imports an animations which forces the models to 0,0,0. If you don’t use Animstions, disable the importing in the FBX importer. If you need animations then you should either not animate the root bone or add your object as a child object to an empty GO and use that GO as your character.

edit
If you use the empty gameobject method, you have to attach your script to the empty gameobject since this is now the object. The model is just the visual part attached as child.

If you want your script on your model, you can access the parent like this:

transform.parent.gameObject