- Home /
EnemyDmg not communicating to HealthDraw
EnemyDmg script does not seem to communicate to HealthDraw located in GameObject = "Player". I located HealthDraw in Player, unfortunately EnemyDmg paused the game and posted a NullReferenceException warning. I tried to solve this issue using several variables from Unity Wiki, but they do not work. I am fresh to scripting, so I do not understand how to set up proper communication parameters. Any solution to this problem will be greatly appreciated:
EnemyDmg Source Snippet:
var target: GameObject;
//rest of code omitted
function Attack ()
{
var direction = Vector3.Dot(target.transform.position - transform.position, transform. forward);
var distance = Vector3.Distance(target.transform.position, transform.position);
var go = GameObject.Find("Player");
go.GetComponent(HealthDraw).EnemyDmg(-damage);
}
NullReferenceException was displayed for last line.
HealthDraw Receiver Snippet:
var Health = 100;
//rest of code omitted
function EnemyDmg(adjust)
{
Health = Health+adjust;
}
If you need full scripts, ask me. These snippets hopefully are enough for diagnostics and producing a solution.
THANKS!!
Sorry, can't seem to comment on my answer below... I realized it's kind of mis-worded: if Find("Player") is not successful, the var "go" remains empty, but there would be no error, until we try to access go in the next line, so the null reference would still come up at the SECOND line, not the first. Can't seem to edit my answer though, so I thought I'd clarify.. I strongly suspect that is the issue.. Also, the null reference message should give you that info, of what's null, in this case something like: object "go" not set to instance of an object (telling you that "go" is what has the null value.
Answer by Seth-Bergman · Jul 04, 2012 at 06:56 PM
Is the Null reference for go, or HealthDraw? I would start by splitting the GetComponent line:
var go = GameObject.Find("Player");
var script = go.GetComponent(HealthDraw);
script.EnemyDmg(-damage);
if it's the top line, GameObject.Find is the prob. This looks for the name of the object in the scene, as opposed to the tag, as in GameObject.FindWithTag.
if it's the middle line, recheck the name of the script (HealthDraw).
if it's the third, the function EnemyDmg I guess... but it's probably the first I think..
EDIT:
I realized my answer above is kind of mis-worded: if Find("Player") is not successful, the var "go" remains empty, but there would be no error, until we try to access go in the next line, so the null reference would still come up at the SECOND line, not the first. I strongly suspect that is the issue.. Also, the null reference message should give you that info, of what's null, in this case something like: object "go" not set to instance of an object (telling you that "go" is what has the null value).
Glad I could help!
Seth, thanks!! you pointed out to me that it was a self-inflicted script error. I changed the GameObject.Find variable into GameObject.FindWithTag to allow for scripted selection based on tagging (original intent). I haven't clarified more about the intent earlier, but the player was just a ThirdPersonController, unlike the string stated next to GameObject.Find. I found out the problem, and it's now a lesson learned.
Asimov, your suggestion was a good one. "Send$$anonymous$$essage" is something that my head can wrap around for the moment, and will be used for future scripts.
Answer by asimov · Jul 04, 2012 at 06:40 PM
For the line:
go.GetComponent(HealthDraw).EnemyDmg(-damage);
I would do the following:
go.GetComponent(HealthDraw).SendMessage("EnemyDamage", -Damage);
Give that a try.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073715im_/https://answers.unity.com/themes/thub/images/avi.jpg)