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 Majinsarek · Nov 29, 2013 at 06:49 AM · javascriptnullreferenceexception

NullPointerException while changing a variable from one script in another

Hello, So I am trying to increment the score in one of my scrips when the person hits a coin. I think I may be linking my scripts wrong as I have been getting this error all night. I have tried a ton of different methods and have no idea. The tricky part being that the PlayerInfo script is on the player while the CoinOnCollision script is on the coin. any ideas?

PlayerInfo script

 #pragma strict
 
 var currentHealth : int = 10;
 var maxHealth : int = 10;
 public var currentScore : int = 0;
 
 function RaiseScore(score : int) {
     this.currentScore += score;
 }
 
 function Update () {
 
 }

CoinOnCollision script

 #pragma strict
 
 var playerInfoScript : PlayerInfo;
 
 function Start() {
     playerInfoScript = GameObject.FindGameObjectWithTag("Player").GetComponent("playerInfoScript");
 }
 
 function OnTriggerEnter() {
     playerInfoScript.currentScore += 10;
     Destroy(gameObject);
 }
Comment
Add comment · Show 3
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 Spinnernicholas · Nov 29, 2013 at 07:36 AM 0
Share

Can you paste in the error messages please?

avatar image fafase · Nov 29, 2013 at 07:50 AM 0
Share

Line 3 and line 6. The variable is of type PlayerInfo, the GetComponent method is passed a string "playerInfoScript". This is the error.

avatar image Majinsarek · Nov 29, 2013 at 04:29 PM 0
Share

the error I'm getting is NullReferenceException: Object reference not set to an instance of an object CoinOnCollision.OnTriggerEnter () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CoinOnCollision.js:9)

1 Reply

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

Answer by fafase · Nov 29, 2013 at 07:33 AM

playerInfoScript or PlayerInfo?

The variable shows it should be a PlayerInfo but you are passing playerInfoScript

Also, you can remove the "" for the parameter it works better. Or add

 GetComponent("ScriptName") as ScritpName;
Comment
Add comment · Show 5 · 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 Majinsarek · Nov 29, 2013 at 07:02 PM 0
Share

doesn't the way I made it say a new variable called playerInfoScript of type PlayerInfo? or should it be the other way around. I seem to have using GetComponent no matter when I use it by I will try it that way. thanks :)

avatar image fafase · Nov 29, 2013 at 08:36 PM 0
Share

What is the name of your other script? That is what you need to use as type and as parameter for the GetComponent

avatar image Majinsarek · Nov 29, 2013 at 08:48 PM 0
Share

the script that contains the variable I'm trying to change is called PlayerInfo and the script I am currently working in is CoinOnCollision

avatar image fafase · Nov 29, 2013 at 08:57 PM 0
Share
 var script :PlayerInfo;
 
 function Start(){
    script = GameObject.FindGameObjectWithTag("Player").GetComponent(PlayerInfo);
 }
 
 function OnTriggerEnter() {
     script.currentScore += 10;
     Destroy(gameObject);
 }
avatar image Majinsarek · Nov 29, 2013 at 09:09 PM 0
Share

It works! thank you so much! I see what I was doing wrong now too :) your are a saint

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

17 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

Related Questions

"NullReferenceException" Error when trying to count objects with a particular tag using GameObject.FindGameObjectsWithTag function 1 Answer

Another Null Reference Exception 1 Answer

Help with eval() 0 Answers

NullReferenceException: Object reference not set to an instance of an object 2 Answers

NullReferenceException: Object reference not set to an instance of an object 3 Answers


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