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 digitaldebonaire · Apr 17, 2014 at 07:51 PM · nullreferenceexceptionscoreadding

Having trouble updating the text in my GUI

I am making a tower defense game. I want the total amount of damage dealt to be kept track of. I have a variable called scoreCount where I am storing it in a script that I have named LevelMaster that controls game states and gui updating.

 var scoreCount : float = 0;

 scoreText.text = "Score: "+scoreCount;

in a second script that I have for my turrets I have this damage function using raycasts

 var hit : RaycastHit;
         
             if (Physics.Raycast(rayorigin.position, rayorigin.forward, hit, rayRange, 1<<13)) {
                 if(hit.collider != null ){
                     if (hit.collider.tag == "Ground Enemy") {
                         hit.collider.SendMessage("TakeDamage", myDamageAmount, SendMessageOptions.DontRequireReceiver);
                         Debug.Log("projectile fired");
                         levelMaster.scoreCount += myDamageAmount;
                         
                     }
                     else {
                         Debug.Log("Hit a different collider");
                     }
                  }
              }

my issue is in the second script where I have levelMaster.scoreCount += myDamageAmount;

I am getting a NullReferenceException from that line. I have var levelMaster : LevelMaster; declared at the top of the turret script so it has access to the LevelMaster script.

I want myDamageAmount to be added to the scoreCount variable every time that chunk of code dealing damge is run.

Can anyone tell me what I can do to fix this?

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 robertbu · Apr 17, 2014 at 08:18 PM

Declaring the variable does not give you access to the LevelMaster script. You have to initialized 'levelMaster'. You can do it my first selecting the game object with the obove script and dragging and dropping the game object that has the LevelMaster scrip onto the 'levelMaster' variable in this script in the Inspector.

Alternately you could do it dynamically. In start, you can add:

 levelMaster = GameObject.Find("NameOfLevelMaterGameObject").GetComponent(LevelMaster);

If you've initialized by dragging and dropping and you still get the null reference error, search your Hierarchy to see if you have the above script attached to more than one game object.

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
avatar image
0

Answer by Burla · Apr 17, 2014 at 08:27 PM

If your variable scoreCount isn't already both public and static you could do that, which makes it accessible from the second script.

The NullReferenceException can easily be solved by using the function GetComponent(). The way to do that would be to declare a variable in the top of your script.

 var levelMaster : LevelMaster;

The above code is what you already have.

In the Start() function you should put the following code.

 levelMaster = GetComponent<LevelMaster>();

This should make the reference correct. The above is similar to instantiating a class which is shown below.

 var levelMaster = new LevelMaster();

If anything of what I've written is wrong, please feel free to correct me. Let me know if this solved your problem.

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

21 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

Related Questions

How do i add points when the object past a specific coordinate 1 Answer

NullReferenceException thrown when trying to access a function from another GameObject 2 Answers

Why this Error when trying to add a score using the new GUI ? 0 Answers

How do I stop adding to a variable? 1 Answer

List or Array for Rays 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