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…