Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Great Alexander · Jan 18, 2013 at 08:57 AM · nulloop

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");
         }
     
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 12 · 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 Great Alexander · Jan 18, 2013 at 09:17 AM 0
Share

if you can show me in code, you would be so generous

avatar image Great Alexander · Jan 18, 2013 at 09:26 AM 0
Share

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 );
avatar image fafase · Jan 18, 2013 at 09:27 AM 0
Share

Use AddComponent ins$$anonymous$$d. This:

  Walk walk = new Walk();

becomes:

  Walk walk = gameObject.AddComponent<Walk>();
avatar image Great Alexander · Jan 18, 2013 at 09:33 AM 0
Share

I did and I recieve Null Refrence in this line:

    controller.$$anonymous$$ove (moveDirection * Time.deltaTime * walkSpeed );

in the Walk class

avatar image SolidSnake · Jan 18, 2013 at 09:49 AM 1
Share

@fafase why should he call walk.Update()?

Once the $$anonymous$$nobehaviour (Walk) is attached wouldn't the Update function inside Walk be called automatically?

Show more comments
avatar image
0

Answer by Imankit · Jan 18, 2013 at 09:34 AM

Vector3 moveDirection = new Vector3();

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Animation Error "Null reference exception"? 2 Answers

c# 2D array error: need Help! 1 Answer

Resources.Load texture problem 1 Answer

Fresh installed , with problems 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