- Home /
SendMessage is working but the variable is not being updated on the other end
var maxDist : float = 1000000000;
var decalHitWall : GameObject;
var floatInFrontOfWall : float = 0.00001;
var damage : float = 100;
var bulletForce : float = 1000;
function Update ()
{
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit))
{
if (decalHitWall && hit.transform.tag == "Level Parts")
Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
if (hit.transform.tag == "Enemy")
{
Debug.Log("hit enemy");
hit.transform.SendMessage("ApplyDamage", damage);
}
}
Destroy(gameObject);
}
My Bullet script sends a message to the enemy that is hit...
var startingHealth = 100;
private var enemyHealth : int;
function Start () {
enemyHealth = startingHealth;
}
function ApplyDamage(damage : int) {
Debug.Log("taking hit");
enemyHealth -= damage;
if (enemyHealth <= 0) {
Destroy (gameObject);
}
}
The ApplyDamage function is successfully called. The issue however is that enemyHealth does not change??? This script is attached to the enemy which is tagged 'Enemy'.
Debug logs show that it should be working: hit enemy UnityEngine.Debug:Log(Object) BulletScript:Update() (at Assets/Scripts/BulletScript.js:16)
taking hit UnityEngine.Debug:Log(Object) EnemyHealth:ApplyDamage(Int32) (at Assets/Scripts/EnemyHealth.js:9) UnityEngine.Component:SendMessage(String, Object) BulletScript:Update() (at Assets/Scripts/BulletScript.js:17)
What am I missing? This was working in another scene.
What is the value of "damage" in the ApplyDamage function? Try debugging it out. It might have been set to something else than 100 in the inspector.
Answer by oliver-jones · Dec 19, 2013 at 09:11 AM
It could be that the receiving end is expecting an INT, whereas your sending a FLOAT. Your actual health on the enemy is also an INT. Pick one and stay with it.
Although, theoretically putting a float into an int should work - but try it anyway.
Also, take away the 'private' away from the enemyHealth var as it won't work if you have more than one instance of this (more enemies around the scene), as they will be 'sharing' this value.
Peace
Your answer
Follow this Question
Related Questions
Damage script is screwed up...? what to do? 1 Answer
FPSTutorial: AI robot not taking any damage! 2 Answers
Weapon Mesh Damage 1 Answer
How to get Enemy to break wall between it and Player. 1 Answer