- Home /
Errors on Bullet script
Hi I have done a script for a bullet by using documentation of unity but I have a couple of errors. Here you have the code:
// - instantiates a explosion prefab when hitting a surface // - then destroys itself
var explosionPrefab : Transform;
function OnCollisionEnter(collision : Collision) { // Rotate the object so that the y-axis faces along the normal of the surface var contact : ContactPoint = collision.contacts[0]; var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal); var pos : Vector3 = contact.point;
// Kill ourselves
Destroy(gameObject);
SendMessage ("ApplyDamage", 1);
clone = Instantiate(explosionPrefab, pos, rot);
// Destroy the projectile
Destroy (gameObject);
}
And here you have the errors:
The referenced script on this Behaviour is missing!
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
ColisionBala:OnCollisionEnter(Collision) (at Assets/nuevo/ColisionBala.js:17)
And:
The referenced script on this Behaviour is missing!
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
$:MoveNext() (at Assets/nuevo/EnemyRangedAttack.js:170)
And:
SendMessage ApplyDamage has no receiver!
UnityEngine.Component:SendMessage(String, Object)
ColisionBala:OnCollisionEnter(Collision) (at Assets/nuevo/ColisionBala.js:16)
Anyone can help me? I achieve the explosion but the player don't loose life >__<'
Thanks for all
Answer by Mortennobel · May 09, 2011 at 09:17 AM
Only destroy the gameObject once as the last thing in your script.
Edit: You should also use
collider.gameObject.SendMessage ("ApplyDamage", 1);
instead of just SendMessage, since you want to call ApplyDamage to the player and not the bullet itself. The player also needs a to have a component attached with a function of the signature:
function ApplyDamage(var v : Integer){
}
And the player also need to have the collider object on the same GameObject as the script with the ApplyDamage function.
Same problems with this. And when i wrote this post I forgot to write one of the errors (the one regarding to send message)
Your answer
![](https://koobas.hobune.stream/wayback/20220613132825im_/https://answers.unity.com/themes/thub/images/avi.jpg)