Trying to create an object (cannonball) and instantiating that object. I have success doing it and making it shoot towards the target.
My problem is, when I try to Destroy the cloned object, it will only destroy itself after I hit the fire button again and the object only goes so far and stops. below is the script I am playing with(modified the shootballFPS)
Also, If I try and shoot multiple times, the objects doesn’t get destroyed instead it creates a clone of the clone.
var target:Transform; //target is Player
var ball : Rigidbody; //the cannon ball
var velocity = 30.0;
function Update ()
{
var autoDestroySeconds = 5;
if (Input.GetButtonDown ("Fire1"))//change this to OnCollisionEnter
{
transform.position.x = 123.0061;
transform.position.y = -3.675993;
transform.LookAt(target);
var shoot : Rigidbody = Instantiate (ball,transform.position, transform.rotation);
shoot.velocity = transform.rotation * Vector3.fwd * velocity;
Destroy(shoot, autoDestroySeconds);
}
}
function Start ()
{
Physics.IgnoreCollision(target1.collider,collider); //no collision on wall3
}
What I’m trying to do is shoot the player, when hit,cannonball explodes(replaced by explosion) and cannonball is destroyed.
Also attached below is the screen shot of the cannon ball object, it only goes so far and stops.
Also I have a system.null reference exception,
UnityEngine.object:instantiate(UnityEngine.object.original, Vector3 position, quaternion rotation) and is highlighting the
You probably want to look into why you get this null reference exception. Possibly there is no cannon ball assigned in your launcher? Or the bullet doesn’t have a explosion assigned?
By double clicking on the error in the status line it will get you to the script and the line where the error occurred. By clicking once on the error, it will show in the console and you can see the entire trace of functions involved in the error.
Launcher??
cannon ball is a sphere housed inside a wall(hiding) which ignores the collision with this wall.
CannonBall has a Rigidbody with the gravity and isKinematic unchecked and also has a spehere collider attached.
No explosion assigned yet.
Played with the script and made some minor changes.
Here’s the script
var target:Transform; //target is Player
var ball : Rigidbody; //the cannon ball
var target1:Transform; //wall where cannonball is housed
var velocity = 30.0;
function Update ()
{
if (Input.GetButtonDown ("Fire1"))//change this to OnCollisionEnter or OnTrigger
{
transform.position.x = 123.9653;
transform.position.y = 1.360522;
transform.LookAt(target);
var shoot : Rigidbody;
shoot = Instantiate (ball,transform.position, transform.rotation);
shoot.velocity = transform.TransformDirection(Vector3.fwd * velocity);
Destroy(shoot,5);
}
}
function Start ()
{
Physics.IgnoreCollision(target1.collider,collider); //no collision on wall3
}
Still getting the Null reference exception error this time pointing to
Also, The instantiated ball should be destroyed in 5 seconds using “Destroy(shoot,5);”
Instead, it get destroyed after I hit the fire button again but none of the cloned objects are being destroyed in the inspector.
I don’t think that I am using the destroy thingy properly.
Hmmm, I don’t understand.
Well here’s is the screenshot of the inspector.
I’m still wondering why is the cloned object not being destroyed after 5 seconds.
Played with the inspector and added the shootbal prefab from the physics demo and I got rid of the exception null error but the cloned object is still not being destroyed.
You are cloning the ball which is a reference to a rigidbody, so the object you get back is a rigidbody.
Thus Destroy will delete the rigidbody from the game object.
What you want to do instead is:
Destroy(shoot.gameObject, 5);