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 SithArrow · Mar 11, 2013 at 03:54 PM · javascriptfunctionreferencenullexception

Why is this code getting NullReferenceException

I have this code that refers to another script called PlayerStatus.js which includes a simple function addPuz() but in game, it tells me there is this error. I've been looking for ages and trying things such as using GameObject or GetComponent and as but nothing seems to work. I'm not very experienced but this seems pretty simple of calling a function in another script so I'm not sure why it won't work.

 var Object1 : Transform;
 var Object2 : Transform;
 var dist = 1;
 
 function Update ()
 {
     
 }
 
 if(Vector3.Distance(Object1.transform.position, Object2.transform.position) < dist)
 {
     var playerStatus : PlayerStatus;
     playerStatus.addPuz(); 
     Destroy(this);
 }
Comment
Add comment · Show 2
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 Nelis · Mar 11, 2013 at 04:07 PM 1
Share

Don't you need your if statement in the update function?

avatar image fafase · Mar 11, 2013 at 04:10 PM 2
Share

And the playerStatus is just a reference to a PlayerStatus type but there is nothing in there. So you need to find the object you want to deal with or you instantiate one.

Also, Destroy(this); will destroy the script, not the object.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by BilboStabbins · Mar 11, 2013 at 04:50 PM

Hi SithArrow,

Assuming that the playerStatus script is attached to the Player, you would first need a reference to the Player object to be able to reference the script. What you've done by saying, var playerStatus PlayerStatus is created an instance of A PlayerStatus script, that's all, not one attached to anything. To reference a specific script instance you have to reference the object its attached to first, then use GetComponent to access the script attached to it.

So, something like:

 function Update()
 {
     // Reference the PlayerStatus script attached to Player (gameObject) and 
     //  assign it to playerStatus instance
     playerStatus = gameObject.GetComponent(PlayerStatus);
 
     // Note, GetComponent(PlayerStatus) will also work as 'gameObject.' is assumed 
     //  if nothing is placed before GetComponent
 
     // Use the instance to call the addPuz() function
     playerStatus.addPuz();
 }

Edit - Yes, Fafase and Nelis are correct (wasn't looking at that, d'oh!). It should be in Update(), and the Destroy function will destroy the script. To destroy the object the script is attached to, use:

 Destroy(gameObject);

I hope that helps.

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 SithArrow · Mar 11, 2013 at 08:44 PM

Took me a bit to figure it out but where you put gameObject I realized i needed to make a var object and then drag over the player in Unity or atleast I worked out that way. So thank you very much. I do realize Destroy(this) only destroys the script and that's what I want. Also I'm not exactly sure why it should be in the update bit because either way it should only run through once. Or I'm just missing the point of the update function. All in all it works now so thank you very much for tending to my ignorance.

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 Polinator · Mar 11, 2013 at 08:52 PM 0
Share

It does run once. It runs when the distance between the two objects are less than your distance variable.

It should go in the update function because it has to CHEC$$anonymous$$ if the distance between the two objects are less than variable distance, EVERY frame. So, that's why it should be in Update().

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

13 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

Related Questions

NullReferenceException Error 6 Answers

Instantiate() as GameObject = null reference 1 Answer

Null Reference Exception error 3 Answers

Null Reference exception error 2 Answers

NullReferenceException - With Network PlayerSpawn 0 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