Question by
Cyborgast · Jan 31, 2020 at 10:22 PM ·
gameobjectinstantiate
Why is my object not instantiating multiple times?
I'm trying to instantiate a copy of a prefab, bulletPre, when a key is pressed but only one object will load at a time, a new one won't instantiate until the old one is destroyed. How do I fix this? Here is my code for the "Player" object that spawns the bullet:
public class Player : MonoBehaviour {
private Transform myTransform;
public GameObject bulletPre;
void Start()
{
//Sets vectors here//
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space")) {
Instantiate(bulletPre, myTransform.position, Quaternion.identity );
}
}
}
and in Bullet:
public class Bullet : MonoBehaviour
{
private Transform myTransform;
public int bulletSpeed = 6;
// Start is called before the first frame update
void Start()
{
myTransform = transform;
}
// Update is called once per frame
void Update()
{
// The bullet travels up in a controlled environment
{
myTransform.Translate(Vector3.up * bulletSpeed * Time.deltaTime);
}
{
if (myTransform.position.y > 9)
Destroy(gameObject);
}
}
}
If I setActive(false) on the bullet after spawning, multiple clones spawn but as soon as multiple are activated they disappear and only one instance spawns.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612222443im_/https://answers.unity.com/themes/thub/images/avi.jpg)