Hello brothers from other mothers!
I have created a barrel which should explode when some requirements are met. It should also deal some damage based on distance, but for some reason SendMessage(which sends the damage information) causes Unity to crash and I don’t know why.
Here is my code if you want it:
var health = 100.0;
var explosion : GameObject;
var expSounds : AudioClip[];
var leakSounds : AudioClip[];
var explosiveRadius = 5.0;
var explosivePower = 100.0;
var force = 200.0;
var Damage = 150.0;
var fireSpew : ParticleEmitter;
var deadReplacement : Transform;
function ApplyDamage (damage : float){
health -= damage;
if(health <= 25){
if(fireSpew){
fireSpew.emit = true;
Invoke("Kill", 3);
audio.PlayOneShot(leakSounds[Random.Range(0,leakSounds.Length)]);
}
}
if(health <= 0){
Kill();
}
}
function Kill (){
var explosionPos : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, explosiveRadius);
Instantiate (explosion, transform.position, transform.rotation);
audio.PlayClipAtPoint(expSounds[Random.Range(0,expSounds.length)], transform.position);
for (var hit : Collider in colliders) {
if (!hit){
continue;
}
if (hit.rigidbody){
hit.rigidbody.AddExplosionForce(explosivePower, explosionPos, explosiveRadius, 1.0);
var proximity = Vector3.Distance(transform.position, hit.transform.position);
var effect : float = 1.0 - (proximity / explosiveRadius);
hit.SendMessage("ApplyDamage",Damage*effect, SendMessageOptions.DontRequireReceiver);
}
}
var REP = Instantiate(deadReplacement, transform.position,transform.rotation);
REP.rigidbody.AddForceAtPosition(Vector3(Random.Range(-1000,1000),2000, Random.Range(-1000,100)), transform.position);
Destroy(gameObject);
}
The code gives no errors and I do not have any problems/crash/bugs using SendMessage on anything else then the barrels. Even if I copy and paste my script from other objects the it still crashes Unity.
If any of you could have some idea of what is causing the crashes and possible any solutions to them I would be very thankful.
Thank you.