Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by BadwolfX · Jul 25, 2017 at 03:47 PM · scripting problemnot workingscripterror

NullReferanceException When Enemy touches Player

Hello - Will here, After creating a short script (supplied bellow) I have noticed an error occurring: When the enemy touches the player, rather than taking the 10 health away, an error message is broadcasted; "NullReferanceException: Object Referance not set to an instance of an object." I am assuming this is what is stopping the health from being taken away. Many Thanks, Will.

var health = 100;

var healthcount : Transform;

var enemy : Transform;

function Update () {

     healthcount.transform.GetComponent.<TextMesh>().text = health.ToString();

}

function OnTriggerEnter (other : Collider) {

     if (other.Collider.enemy){
     health -= 10;
     }

}

Comment
Add comment
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

2 Replies

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

Answer by Daemonhahn · Jul 25, 2017 at 03:55 PM

Because you are getting the component in Update, it is likely not available yet.

Instead cache this reference in Start so you dont get it each time.

Like this:

      Text healthText;
 
 void Start()
 {
 healthText = healthcount.transform.GetComponent.<TextMesh>();
 }
 
 void Update()
 {
 healthText.text = health.ToString();
 }
 
 

Comment
Add comment · Show 6 · 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 BadwolfX · Jul 25, 2017 at 04:02 PM 0
Share

I attempted a change: var health = 100; var healthcount : Transform; var enemy : Transform; var healthText; function Start() { healthText = healthcount.transform.GetComponent.(); }

function Update() { healthText.text = health.ToString(); }

function OnTriggerEnter (other : Collider) { if (other.Collider.enemy){ health -= 10; } }

However I still get the error, you used void, I tried my best to translate to JavaScript.

avatar image Daemonhahn BadwolfX · Jul 25, 2017 at 04:13 PM 1
Share

But you are trying to get (other.Collider.enemy) and yet you do not do a get component to get the enemy? How are you getting the .enemy, as it looks to me like it would give a null reference as there is no reference for it.

Also try learning c#, you will find it more robust and a lot more tutorials use c# than javascript.

avatar image Daemonhahn Daemonhahn · Jul 25, 2017 at 04:15 PM 0
Share

something like

 Enemy enemy = other.Collider.GetComponent<Enemy>();
 if(enemy)
 {
  // rest of code 
 }
 
Show more comments
avatar image
0

Answer by wizbit · Jul 25, 2017 at 03:52 PM

Hey BadwolfX

i believe it may be that you have not set the within your healthcount.transform.GetComponent.().text = health.ToString().

https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

Comment
Add comment · Show 2 · 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 BadwolfX · Jul 25, 2017 at 04:02 PM 0
Share

The page is unfortunately all C#?

avatar image Daemonhahn BadwolfX · Jul 25, 2017 at 04:17 PM 0
Share

I recommend learning C# as javascript is not widely used for Unity anymore and has many caveats

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

113 People are following this question.

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

Related Questions

Linking Image to item list 1 Answer

Can anyone just read this over? for my 2d runner? 1 Answer

SphereCast doesn't work when I increase the radius. 1 Answer

Coin collection script (not working) 1 Answer

Roll a Ball wont roll? 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