- Home /
I recieve Null Refrence Exception when I use OOP programming
I am trying to organize my game with OOP , I have the Main class which is attached to the Player and call from inside it the Walk function, here is the the Main Class:
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
public bool isDead;
Walk walk = new Walk();
// Use this for initialization
void Start () {
isDead = false;
}
// Update is called once per frame
void Update () {
walk.Moving();
}
}
and when the game runs I recieve Null Refrence Exception on that line and the code do not work: moveDirection = transform.TransformDirection(Vector3.forward);
and here is all the code for the Walk Class which includes the Moving function:
using UnityEngine;
using System.Collections;
public class Walk : MonoBehaviour {
Vector3 moveDirection;
CharacterController controller;
float walkSpeed;
float runSpeed;
// Use this for initialization
void Start () {
moveDirection = Vector3.zero;
walkSpeed = 2f;
runSpeed = 5f;
}
// Update is called once per frame
void Update () {
controller = (CharacterController)transform.GetComponent (typeof(CharacterController));
transform.Rotate (0,2 * Input.GetAxis("Mouse X"),0);
Moving();
}
public void Moving()
{
if(Input.GetKey ("up"))
{
moveDirection = transform.TransformDirection(Vector3.forward);
controller.Move (moveDirection * Time.deltaTime * walkSpeed );
animation.CrossFade ("walk");
}
if(Input.GetKey (KeyCode.LeftShift) && Input.GetKey (KeyCode.UpArrow))
{
moveDirection = transform.TransformDirection (Vector3.forward);
controller.Move (moveDirection * Time.deltaTime * runSpeed );
animation.CrossFade ("run");
}
if(Input.GetKey ("down"))
{
moveDirection = -transform.TransformDirection(Vector3.forward);
controller.Move (moveDirection * Time.deltaTime * walkSpeed );
animation.CrossFade ("walk");
}
}
}
Answer by SolidSnake · Jan 18, 2013 at 09:02 AM
If your class is a inheriting from Monobehaviour you cant simply use new to create an instance and you should receive warning ... the script need to be attached to a gameobject
http://answers.unity3d.com/questions/33460/new-monobehaviour-warning.html
==EDIT== You can either add the component to the same game object:
gameObject.AddComponent("Walk");
then when you want to call the functions you can:
gameObject.GetComponent("Walk").Moving();
other way of referencing the script is by declaring a variable of Type "Walk" to be more efficient. and reference it in the Start() function of you Main script.
But since you are calling moving inside the update function of Walk there is no point of calling this function again from your Main script.
if you can show me in code, you would be so generous
I wrote the following code: public class $$anonymous$$ain : $$anonymous$$onoBehaviour {
public bool isDead;
GameObject newGo;
Walk walk;
// Use this for initialization
void Start () {
newGo = new GameObject("Spartan$$anonymous$$ing");
walk = (Walk)newGo.AddComponent(typeof(Walk));
isDead = false;
}
and I recieve the Null Refrence on that line now:
controller.$$anonymous$$ove (moveDirection * Time.deltaTime * walkSpeed );
Use AddComponent ins$$anonymous$$d. This:
Walk walk = new Walk();
becomes:
Walk walk = gameObject.AddComponent<Walk>();
I did and I recieve Null Refrence in this line:
controller.$$anonymous$$ove (moveDirection * Time.deltaTime * walkSpeed );
in the Walk class
@fafase why should he call walk.Update()?
Once the $$anonymous$$nobehaviour (Walk) is attached wouldn't the Update function inside Walk be called automatically?