Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by blackmethod · Jul 09, 2011 at 11:17 PM · rigidbodynullreferenceexceptionreference

NullReferenceException Error, has not been solved yet.

I posted this a while ago, but since the post is old, i'm no longer getting answers. And I haven't received an answer that has fixed the problem.

What I want my script to do is make the ball move by the means of force and torque, but it doesn't move anywhere and I get this error whenever I run it.

NullReferenceException Movementscript.FixedUpdate () (at Assets/Movementscript.js:7)

Here is the script for the ball that is supposed to move:

 var speed=20.0; 
 function FixedUpdate () {
 
 var torque=Vector3(Input.GetAxisRaw("Vertical"), 0, -Input.GetAxisRaw("Horizontal"));
 if(torque.magnitude > 0.0){
     torque=Camera.mainCamera.transform.TransformDirection(torque);
     rigidbody.angularVelocity=Vector3.Lerp(rigidbody.angularVelocity, torque * 2, 0.3);
 }
 if (Input.GetKey("w")) {
 
 rigidbody.AddRelativeTorque (2, 0, 0);
 
 rigidbody.AddRelativeForce (2, 0, 0); }
 
 if (Input.GetKey("s")) {
 
 rigidbody.AddRelativeTorque (-2, 0, 0); rigidbody.AddRelativeForce (-2, 0, 0); }
 
 if (Input.GetKey("a")) { rigidbody.AddRelativeTorque (0, 0, -2); rigidbody.AddRelativeForce (0, 0, -2); }
 
 if (Input.GetKey("d")) { rigidbody.AddRelativeTorque (0, 0, 2); rigidbody.AddRelativeForce (0, 0, 2); } }





And here is the script for the camera that is attached to it:

 var target: Transform;
 var damp: float = 0.2;
 var distance: float = 50;
 
 function Update(){
 
     damp = Mathf.Clamp(damp, 0.01, 10); // clamps damping factor
     var pCam = transform.position;
     var pTarget = target.position;
     var diff: Vector3 = pTarget - pCam;  // diff = difference between positions
     var dist = diff.magnitude;  // dist = distance between them
     if (Mathf.Abs(diff.y) < 0.7*distance){
         diff.y = 0;  // doesn't modify camera height unless angle > 45
     } 
     if (dist>distance){ // if distance too big...
         diff *= 1-distance/dist; // diff = position error
         // move a FPS independent little step towards the ideal position
         transform.position = pCam + diff * Time.deltaTime/damp;
     }
     transform.LookAt(pTarget);
 }



For reference the name of the camera is mainCamera. And the name of the ball is Sphere. The ball has a rigidbody attached. And lastly the only things in the Scene are the terrain "Terrain", the ball "Sphere", the light "Directional Light", and the camera "mainCamera".

Help on how to fix this problem so I can get rolling. (Pun intended, laugh here if you want to.) would be a great help. Thanks again UnityAnswers.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Jul 09, 2011 at 11:59 PM

@valestrom, I created a new scene, placed the same objects (terrain, sphere+rigidbody, light and mainCamera), attached the scripts to the sphere and the camera, and everything worked fine: the camera run to reach certain distance of the sphere, then followed its movement when I pressed WASD. I saved the camera script initially as Camera.js, but Unity gave a warning that GetComponent would not work because there were a component named Camera already in the project. I renamed it to CameraScript.js and everything worked as expected.
Nothing seems wrong in your script, and in fact it works. My suggestion is: create a new project and scene, place the scripts there and try again. If it still give the same error, try to re-install Unity. There are no rational reasons for this to not work - it worked in my computer (PC Intel Core Duo, Unity 3.3 Indie)

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
avatar image
1

Answer by Bunny83 · Jul 10, 2011 at 12:39 AM

Well why you don't just look at the line 7 where the error occured?

What is in line 7?... this:

 torque = Camera.mainCamera.transform.TransformDirection(torque);

So just check the line if there's something wrong. After just 3 sec i can see what's wrong. The Camera class doesn't have a property / variable called mainCamera. It's called Camera.main. Actually you should get a compiler error... if not then i can add a new point to my list why i never ever would use UnityScript. I don't get why some ppl say it's "easier" when you can bump into pitfalls every few steps. In C# with Visual studio it would complain even before you get a chance to test such corrupt code.

Comment
Add comment · Show 1 · 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 Joshua · Jul 10, 2011 at 01:03 AM 1
Share

Actually, I vaguely remembered that property existing. After looking it up in the docs it is indeed not documenting, but when I decompile UnityEngine I find:

 // UnityEngine.Camera
 public static Camera mainCamera
 {
     get
     {
            return Camera.main;
     }
 }

So my guess is Camera.main replaced Camera.mainCamera, and is no longer documented, but should still work. So that explains why there is no compiler error thrown saying the variable doesn't exist.

Also, saying that this is why UnityScript isn't good is nonsense. It will throw an error. The only pitfall is when you do something like function( var index : int = 0;...) { indec += 5 } it will implicitly see indec as a newly declared variable. And yes, I would like to see that changed - you should use strong typing anyway imho - but it still, to me, outweighs some of the annoyances I have with C#. To each his own, I suppose ;)

[EDIT] @valestrom the obvious facepalm reason could be that your 'main Camera' is not tagged as such

avatar image
0

Answer by blackmethod · Jul 10, 2011 at 04:38 AM

Thanks to all of you, UnityAnswers has proven to be a great ****in resource. The whole, putting it in a new scene worked. For whatever reason it did. And it's flawless, set the torque to 25 and even watch it bounce off the map. You guys are great. Send me an email at blackmethod@gmx.com and you get into my Alpha , if you want.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Scripts help 1 Answer

Using hinge joints to make a balance board 1 Answer

How to call a function from another script - getting error "Object reference not set to an instance of an object" 5 Answers

The infamous: Object Reference not set to an instance of an object 1 Answer

Array values not being initialised unless the object with the (Player) script is selected in the inspector whilst the game is on 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