• 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 SovietBear · May 05, 2015 at 11:49 PM · pickups

Issue with writing variables to other scripts.

I'm working on pickups for my game, and every time I try to write the variable from one script to another it keeps giving me this error:

 NullReferenceException: Object reference not set to an instance of an object. CoinScript.OnTriggerEnter2D(UnityEngine.Collisder2D Trigger)(at Assets/Scripts/CoinScript.cs:11)

Obviously the code would be useful, so here is the script for the pickup:

 using UnityEngine;
 using System.Collections;
 
 public class CoinScript : MonoBehaviour {
 
     public DataKeeperScript dks;
 
     void OnTriggerEnter2D (Collider2D Trigger) {
         if(Trigger.gameObject.tag == "Player"){
             Debug.Log ("Coin collected");
             dks.score += 1;
             Destroy(gameObject);
 
         }
     }
 
 }


And here is the script for the player that keeps the score.

 using UnityEngine;
 using System.Collections;
 
 public class DataKeeperScript : MonoBehaviour {
 
     public int score = 0;
     
 
 }

As you can see, the code is small, and has no frills so far, any help would be appreciated!

-Soviet Bear

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 Key_Less · May 06, 2015 at 12:10 AM 1
Share

$$anonymous$$ake sure you're providing a valid reference to the Data$$anonymous$$eeperScript. As the error suggests, on line 11 dks is null.

1 Reply

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

Answer by scarletshark · May 06, 2015 at 03:31 AM

In your code:

 public DataKeeperScript dks;

You are telling Unity to expose the dks variable in the editor. Make sure that you drag your Player object (or whatever object has the DataKeeperScript) into the dks slot! See below:

Left: No object specified (NULL reference).

Right: Object specified. you're good to go!

dks1 alt text

Alternatively, if there is only one instance of DataKeeperScript in your scene, you could do this instead:

  public class CoinScript : MonoBehaviour {
  
      private DataKeeperScript dks;
 
      void Awake () {
           
           // finds the first instance of DataKeeperScript in your scene
           dks = FindObjectOfType<DataKeeperScript>();
      }
  
      void OnTriggerEnter2D (Collider2D Trigger) {
          if(Trigger.gameObject.tag == "Player"){
              Debug.Log ("Coin collected");
              dks.score += 1;
              Destroy(gameObject);
  
          }
      }
  
  }



dks1.png (19.1 kB)
dks2.png (19.4 kB)
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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 can i correct this error PickUp.js(8,9): UCE0001: ';' expected . insert a semicolonat the end,..(unity5.4.1f1) is my version 1 Answer

Completing game upon pickup count 1 Answer

Multiple Objects Interfereing with each other? 2 Answers

How to stop an item that has already been collected from respawning when player returns to a scene? 0 Answers

How to make a note pickup system in Unity2D? 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