Instantiates 2 or more gameObjects on ButtonUp.

Hello again.

My new problem du-jour is perplexing me. I have a script that Instantiates a sphere on ButtonUp to prevent a stream of them from being produced per Will Goldstein’s tutorials. The sphere is a Rigidbody and has a force applied to it when it is instantiated to give it forward momentum.

Initially it worked as intended, however it now routinely produces 2- sometimes more- of these prefabs with no discernible force added to them. Instead of moving forward, these 2 (or more up to 8) stack on top of each other like some sadistic snowman. I have no idea why they would do this.

Every once in a while it will correctly produce a single sphere that moves as it should and I feel happy, however lately that has not been the norm. I will include the bit of relevant code below:

shooter is a GameObject variable that I have preloaded with my prefab that I want to shoot.

function Shoota(){
 
 
 if(Input.GetButtonUp("Fire1")){
 Instantiate(shooter, transform.position, transform.rotation);
 spawned = true;
 }

}

Please know that my oding skills are somewhat lacking, however I will probably understand any answer. Ideally in my final product Player1 will have their turn and then Player2 will have a go, which is why this needs to work correctly. Sorry if this is poorly worded, its early for me and I’m tired as heck.

Thanks Again,
-C. Griffin

P.S. The movement that I referenced above is in a script placed on the Shooter prefab that adds a force in the update function until it collides with either the ground or another marble, which then destroys said script. I don’t know if this is the correct way to do that, but as I said earlier, “my coding, eet’s not so good”.

your code is lacking some necessary information to be able to answer this. But I figure this will work. Modify your code to use this example:

var canShoot = true; //set a boolean to determine whether the user can shoot or not... prevents multiple instantiations.

function Update() {
if (canShoot)
Shoota();
}

function Shoota() {
if (Input.GetButtonUp("Fire1")) {
Instantiate(shooter, transform.position, transform.rotation);
spawned = true;
canShoot = false;
}
}


// You will need to reset canShoot to "true" if you want to be able to shoot again.  That's easy though and it's more of a preference of where you want to do that.  I'll leave that up to you :)