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…
Store the instantiated ball in a variable, reset this variable to null on destruction, and only spawn a ball if variable==null
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.
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 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.
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.