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
0
Question by CleverBot · Jan 08, 2016 at 08:40 AM · nullreferenceexceptionreferenceinstance

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

so guys im trying to see how many protons are in nucleus and im trying to do this... this is the script of nucleus:

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 public class NUCLEUS : MonoBehaviour {
     public float charge, mass, jump, tome, spd;
     Rigidbody2D myb;
     // Use this for initialization
     void Start () {
         myb = this.GetComponent<Rigidbody2D>();
 
     }
     public nums maynum = new nums (0, 0);
     public class nums{
         public int protnum, neutnum;
         public nums(int howmanyprotons, int howmanyneutrons){
         
             protnum += howmanyprotons;
             neutnum += howmanyneutrons;
 
 
         }
 
     }
     // Update is called once per frame
     void Update () {
         if (Input.GetKey("w")) {
             transform.Translate(Vector2.up * jump * tome*Time.deltaTime);
         }
         if (Input.GetKey("a")) {
             transform.Translate(Vector2.left * jump * tome*Time.deltaTime);
         }
         if (Input.GetKey("s")) {
             transform.Translate(Vector2.down * jump * tome*Time.deltaTime);
         }
         if (Input.GetKey("d")) {
             transform.Translate(Vector2.right * jump * tome*Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.Space)) {
             print(maynum.protnum );
         }
         
         Vector2 move = new Vector2 (CrossPlatformInputManager.GetAxis ("Horizontal"), CrossPlatformInputManager.GetAxis ("Vertical")) * spd;
         
         myb.AddForce (move);
         
 
     }
 }

and this is the script of proton:

 using UnityEngine;
 using System.Collections;
 
 public class Proton : MonoBehaviour {
     
     public float charge, mass, spd;
     public Transform nucleus;
     private NUCLEUS nucl;
 
     // Use this for initialization
     void Awake ()
     {
         nucl = GetComponent<NUCLEUS>();
     }
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         transform.position = Vector2.MoveTowards (transform.position, nucleus.transform.position, spd);
 
     }
 
     void OnTriggerEnter2D(Collider2D other){
         nucl.maynum.protnum++;
         print(nucl.maynum.protnum );
     }
     void OnTriggerExit2D(Collider2D other){
         nucl.maynum.protnum --;
         print(nucl.maynum.protnum );
     }
 
 
 
 }
 

as you can see it gives me NullReferenceException NullReferenceException: Object reference not set to an instance of an object Proton.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Script/Proton.cs:26)

i dont know what to do... please help im stuck....

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 Bonfire-Boy · Jan 08, 2016 at 11:07 AM 0
Share

If the line causing it is nucl.maynum.protnum++, it looks like probably nucl is null. So are you sure the proton's GameObject has a NUCLEUS component? (to be sure, you could check in the Awake function after the GetComponent call, as well as before the line that's throwing the exception).

I'm talking about adding things like if (nucl==null) Debug.LogError("nucl is null");.

avatar image CleverBot · Jan 11, 2016 at 09:38 PM 0
Share

So actually i don't want my proton to have script nucleus because its for gameobject nucleus... i mean it's for it and not for proton. i actually wanted gameobject proton to see that "it crossed the nuclear border" and send to nucleus information about crossing the line. i want to use it for counting how many protons crossed the line because i didn't got an idea about how to count protons with nucleus script. so i decided to actually protons send information to nucleus' script about that. and if single crossing proton would enlarge protnum in nucleus script others would do it to so i would get the exact number of them... like handshake from both side...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KungKras · Jan 10, 2016 at 07:14 AM

Have you tried moving the nums class out of the class to the bottom of the document?

Also this line:

 public nums maynum = new nums (0, 0);

should really be moved to above the start funtion where the other variable declarations are.

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 CleverBot · Jan 11, 2016 at 09:33 PM 0
Share

sorry... it wasnt problem... i think as benfire boy told null is actually problem

avatar image CleverBot · Jan 11, 2016 at 09:42 PM 0
Share

thanks, i really appreciate it

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object ? 1 Answer

Trouble creating reference of image in script. 1 Answer

The infamous: Object Reference not set to an instance of an object 1 Answer

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

Pause Script Suddenly Stops Working 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