- Home /
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....
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");
.
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...
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.
sorry... it wasnt problem... i think as benfire boy told null is actually problem
Your answer
Follow this Question
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