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 DarthViper3k · Sep 26, 2012 at 11:29 PM · movementtransformobject

Simple Character movement giving object error

Everything I've read says I'm doing this properly.. so I have no idea what I'm doing wrong. Its a simple move script. Press W and the camera moves forward. And I'm getting the following error.

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

CameraController.Update () (at Assets/Game/Scripts/Camera/CameraController.js:204) function Update () { var controller : CharacterController = GetComponent(CharacterController);
if(Input.GetKey("w")) { moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); transform.Translate(moveDirection); moveDirection = speed;
// Move the controller controller.Move(moveDirection
Time.deltaTime); }
if(Input.GetKey("s")) { moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); transform.Translate(moveDirection); moveDirection = speed;
// Move the controller controller.Move(moveDirection
Time.deltaTime); }
if(Input.GetKey("a")) { moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); transform.Translate(moveDirection); moveDirection = speed;
// Move the controller controller.Move(moveDirection
Time.deltaTime); }
if(Input.GetKey("d")) { moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); transform.Translate(moveDirection); moveDirection = speed;
// Move the controller controller.Move(moveDirection
Time.deltaTime); }
if (Input.GetButtonDown("Fire2")) { mouseButton2DownPoint = Input.mousePosition; mouseRightDrag = true; }
if (Input.GetButtonUp("Fire2")) { mouseRightDrag = false; mouseButton2UpPoint = Input.mousePosition; }
if (Input.GetButtonDown("Fire3")) { print("Middle Mouse"); mouseButton3DownPoint = Input.mousePosition; mouseMiddleDrag = true; }
if (Input.GetButtonUp("Fire3")) { mouseMiddleDrag = false; mouseButton3UpPoint = Input.mousePosition; if (mouseButton3DownPoint == mouseButton3UpPoint) { ClearSelectedUnitsList(); } } } I was gonna remove all the charactercontroller code and instead use transform.position. But doing that gets me errors about not being able to convert vector 3 into a float. I'm confused here. My apologies.. the error is coming from "Controller.Move(moveDirection * Time.deltaTime);
Comment
Add comment · Show 1
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 CaioRosisca · Sep 26, 2012 at 11:56 PM 1
Share

We can´t know what´s your problem because we don´t know which line is the 204th. Edit your question with this info. An a way to find out which of the variables is null is to do the following, for example, if you haev the line

var1.var2.var3.DoSomething;

do this just above the line that throws the error

print("var1 = "+var1); print("var2 = "+var2); print("var3 = "+var3);

then you can see on your console windows exactly which of the terms is null

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 26, 2012 at 11:56 PM

That's a very weird code! You must use character.Move (preferably) or transform.Translate, not both at the same time. And you don't need to check the keys WASD individually: Input.GetAxis does it at once for you. Your code could be as simple as this:

function Update () { var controller : CharacterController = GetComponent(CharacterController);

 moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 moveDirection *= speed;

 // Move the controller
 controller.Move(moveDirection * Time.deltaTime);

 if (Input.GetButtonDown("Fire2"))
 {
    mouseButton2DownPoint = Input.mousePosition;
    mouseRightDrag = true;
 }

 if (Input.GetButtonUp("Fire2"))
 {
    mouseRightDrag = false;
    mouseButton2UpPoint = Input.mousePosition;
 }

 if (Input.GetButtonDown("Fire3"))
 {
    print("Middle Mouse");
    mouseButton3DownPoint = Input.mousePosition;
    mouseMiddleDrag = true;
 }

 if (Input.GetButtonUp("Fire3"))
 {
    mouseMiddleDrag = false;
    mouseButton3UpPoint = Input.mousePosition;
    if (mouseButton3DownPoint == mouseButton3UpPoint) {
      ClearSelectedUnitsList();
    }
 }

}

Anyway, I could not find anything in the code you've posted that could cause that error. Which one is line 204?
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 DarthViper3k · Sep 27, 2012 at 12:14 AM 0
Share

Aldo, unfortunately.. without get key its not picking up the key being pressed once so ever. I should add... the code WOR$$anonymous$$S.. it just produces that error on controller.$$anonymous$$ove whenever the key is pressed. And without transform.Traslate... it doesn't work at all.

avatar image aldonaletto · Sep 27, 2012 at 10:17 AM 0
Share

Well, this should work, but I suppose that this error is preventing your script to work normally. If the error points to controller.$$anonymous$$ove, then probably the CharacterController isn't being found: you've forgot to add a CharacterController to your character, or it's added to an object and your script is attached to another one, possibly a child or parent - notice that the script and the CharacterController must be attached to the same object! Another possibility: the script was inadvertently attached to more than one object (again, probably a character child).
If you can't see any of these errors, add the following line to your script:

 function Start(){
   if (!GetComponent(CharacterController)){
     print("CC not found - script attached to "+name);
   }
 }

Check if that damned button Collapse isn't active in the Console view, and click run: if the CharacterController isn't being found, the name of the object to which the script is attached will appear in the message, thus you can more easily know what's going wrong.

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

Object not moving,Object not moving in any direction 1 Answer

Move Object to location of Trigger? 1 Answer

Moving an object 1 Answer

Move a object and then return , like loop 2 Answers

How can I move an object physically correct around an other object? 1 Answer


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