Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mikez · Jul 04, 2012 at 06:08 PM · javasourcereceiver

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!!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · Jul 04, 2012 at 07:03 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikez · Jul 05, 2012 at 03:50 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to decrease audio source's volume using script? 1 Answer

Unity client <--> Java Server 1 Answer

How could I make a multiplayer system that works with wifi? 0 Answers

Setting a javascript to function at random frames. 0 Answers

AndroidJavaClass. Call returns null ptr. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges