- Home /
How to shoot only once
I would really apreciate if somebody gave me a script which could help me shoot an object like this: I am using a prefab to shoot a football but i want the shooting to stop until the ball is destroyed.So i need only one ball shooted and when it is destroyed i'll be able to shoot the next one... :/
Answer by homeros · May 26, 2011 at 04:51 PM
Do it like this:
var spawnedBall:GameObject;
when instantiating use it like this:
if(spawnedBall == null)
spawnedBall = Instantiate(prefabBall);
This way it'll check if the gameobject is null first, if it is, it'll instantiate a ball and reference your object. When ball you spawned is destroyed, spawnedBall will be null again.
Note that everything related to shooting the ball (applying a force, swinging the foot, etc.) goes into the body of the "if(spawnedBall == null)", so that it is ONLY executed if you actually are allowed to shoot the ball.
they shouldn't be inside "if(spawnedBall == null)" statement, because he can shoot if the ball is spawned either when it's just spawned or spawned before.
edit: oh I just understand what he wants exactly. dunno why but I thought it was like a football game or like. so you're probably right about if statement.
...or I misinterpreted his phrasing ^^
You would be correct if by "i want the shooting to stop" he means, he doesn't want to spawn any more balls, but still wants to be able to interact with the ball by kicking it around.
Answer by Wolfram · May 26, 2011 at 03:41 PM
Store the instantiated ball in a variable, reset this variable to null on destruction, and only spawn a ball if variable==null
Answer by jokesman · May 28, 2011 at 05:54 PM
Here is what i've done.My problem is that the ball is never destroyed :(:(... Only after it's destruction i want it to be able to shoot. If anyone can help i would really apreciate it.
var shootForce:float;
var parentPlayer:Transform;
var prefabBall:Transform;
var spawnedBall:GameObject;
var check:boolean;
check=true;
function Update()
{
if(Input.GetMouseButtonDown(0) &&(check == true))
{
check=false;
var instantiatedBall = Instantiate(prefabBall, transform.position, Quaternion.identity);
instantiatedBall.rigidbody.AddForce(transform.forward * shootForce);
spawnedBall=instantiatedBall;
Destroy(spawnedBall,2);
check=true;
}
}
Can anyone help please???I don't think it's that difficult ! :(
Answer by jokesman · May 30, 2011 at 04:49 PM
Can anyone help here ?? The only thing i can't do is to destroy the ball !!What's wrong please??How can i use destroy here ? See my code above.
for some reason you don't seem to read our answers? both me and wolfram told this. use spawnedBall == null and delete check == true. it doesn't work anyway in the state you're using it.
Ok thank you ! But do you have any idea why the ball is not destroyed? What's wrong with Destroy(spawnedBall,2)?
I don't know why but what I usually do is like this. I use a script on the spawned object, in it i define an explode function. WaitForSeconds(2);Destroy(gameObject); Then I call it from the start function. This way the ball will be destroyed 2 seconds after its spawned.
As we told you repeatedly, your current approach doesn't work. With Destroy(...,2) you delay the destruction by 2 sec. However, code execution continues immediately, so "check" is immediately cleared, which will spawn a new ball and overwrite spawnedBall on your next mouse click.
In my very first response to an answer you deleted prematurely I would have told you to:
don't use delayed Destroy if you need to combine it with game logic
write a separate coroutine which waits for 2 seconds first and then destroys the object without delay, and resets "check" to true.
You can also skip this "check" variable altogether if you ins$$anonymous$$d follow Speed's and my advice we gave you 4 days ago in the answers above.
Answer by jokesman · Jun 03, 2011 at 04:10 PM
I do it but the ball is never destroyed.Please write the small script about how to destroy the ball ............:( i can't go on with the project and i have to .. Thanks for the answers.
Your answer

Follow this Question
Related Questions
How to shoot a bullet to curser position? 2 Answers
Shoot Script 1 Answer
My object acts wierd, 2 balls being shot instead of one (from different position) 2 Answers
Shooting problem - bullet shoots diagonally when facing forward 2 Answers
When flipping player, instantiated objects spawn on different spot. 2D shooting 0 Answers