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.