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
0
Question by andycodes · Feb 15, 2016 at 07:00 AM · rigidbodyvelocitynull reference exceptionobject reference

object reference not set to an instance of an but works with other script

this is probably a common problem that i will say "oh duh" to once its solved. but i keep getting a reference error when trying to access the magnitude of the rigidbody of another game object. my code for the object being referenced looks like this: PlayerController.cs

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speed;
     public float jump;
     public Rigidbody rb;
     private bool isGrounded;
 
     public GameObject target;
     // target is the object you will take the rotations from

     void Start () {
         rb = GetComponent<Rigidbody> ();
     }
 
     void FixedUpdate () {
         
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         if (isGrounded == true) {
             rb.AddForce (moveVertical * target.transform.forward * speed);
             rb.AddForce (moveHorizontal * target.transform.right * speed);
 
             if (Input.GetKeyDown ("space")) {
                 rb.AddForce (0.0f, jump, 0.0f);
             }
         } 
         else {
             rb.AddForce (moveVertical * target.transform.forward * speed * 0.5f);
             rb.AddForce (moveHorizontal * target.transform.right * speed * 0.5f);
         }
     }
     void OnCollisionStay(Collision collision) {
 
         if (collision.gameObject.tag == "Ground") {
             
             isGrounded = true;
             //print ("Grounded");
         }
     }
     void OnCollisionExit (Collision collision) {
         
         if (collision.gameObject.tag == "Ground") {
 
             isGrounded = false;
             //print ("Not Grounded");
 
         }
     }
 }

the script that im referencing the rigidbody in looks like this: gyroScript.cs

 using UnityEngine;
 using System.Collections;
 
 public class gyroScript : MonoBehaviour {
 
     public GameObject target;
     public float speed = 10f;
     public float rotationSpeed = 100.0f;
     private Vector3 offset;
 
 
     public PlayerController follower;
     public float giveaway;
     public float playerSpeed;
 
 
     void Start () {
         offset = transform.position - target.transform.position;
         follower = GetComponent<PlayerController> ();
 
     }
 
     void Update()
     {
         mouseOrbit();
 
     }
 
     void LateUpdate () {
         transform.position = target.transform.position + offset;
             //***
         playerSpeed = follower.rb.velocity.magnitude;
             // this is where i get my error***
         print (playerSpeed);
     }
 
     void mouseOrbit()
     {
         float translation = Input.GetAxis("Mouse Y") * speed;
         float rotation = Input.GetAxis("Mouse X") * rotationSpeed;
         translation *= Time.deltaTime;
         rotation *= Time.deltaTime;
         transform.Translate(0, 0, translation);
         transform.Rotate(0, rotation, 0);
     }
 }


ive checked all my references to ensure that they are correct, infact i actually took that code directly from code i placed onto a camera object that worked without error that looked like this: CameraController.cs

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     public PlayerController target;
 
     public float giveaway;
     float playerSpeed;
 
     void start(){
         target = GetComponent<PlayerController>();
     }
 
     void Update (){
 
         playerSpeed = target.rb.velocity.magnitude; //same codebit that causes issues in other script
         print (playerSpeed);
 
         transform.position = new Vector3 (0f, 1.25f, playerSpeed * giveaway *-1); 
         print (transform.position);
     }
 }

i aplologize for the long reads, but i just cant seem to figure out where im going wrong in this script, everything seems like it should work in theory

any help is much appreciated!

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

1 Reply

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

Answer by EmHuynh · Feb 15, 2016 at 07:49 AM

Hello, @andycodes!

In CameraController.cs, your Start function is declared with the lowercase s. Which explains why it worked and didn't for gyroController.cs. Now, let's find out what your issue is.

Here's a portion of gryoController.cs:

     public PlayerController follower;
     ...
 
     void Start() {
         ...
         follower = GetComponent< PlayerController >();
     }

You have two ways of setting the value of follower.

  1. You can set it in the inspector.

  2. Or within function Start ( follower = GetComponent< PlayerController >(); )

I am sure you set it in the inspector, that's how it your camera (with CameraController.cs attached to it) was able to get access to the Player Controller component of your player object - with a "misspelled" Start function (lowercase s).

The problem is that the value of follower is set to null when`Start` executes because gryoScript is not attached to the same game object as PlayerController script. So it's unable to get the component PlayerController.

Here are some solutions:

  1. Set the value of follower in the inspector and remove the line`follower = GetComponent();` from the Start function of gryoScript.cs.

  2. Attach gyroScript script to the same object as Player Controller script. Let`follower` be initialized in in Start function.

  3. If you don't want gyroScript be attached to same game object as the Player Controller component. You need to declare a new game object variable in it that references the player object and initialize follower by getting Player Controller component from the newly declared game object. Like this:

         public PlayerController target;
         public GameObject player;
     
         void Start() {
             target = player.GetComponent< PlayerController >( );
         }
    
    
    

The problem should be resolve. If there any questions, let us know!

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 andycodes · Feb 15, 2016 at 08:08 AM 0
Share

That did the trick for me!!i was able to use the inspector method to get it to function properly, Thank You!!

avatar image EmHuynh andycodes · Feb 15, 2016 at 08:22 AM 0
Share

You're welcome! Thanks, @andycodes for providing all of the information that we need to fix the issue. They made it easy to begin finding out what the problem was.

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

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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

rigidbody.velocity.normalized application 1 Answer

Is there a way to set velocity while setting the position? 1 Answer

Velocity of multiple rigidbody obiects in array 4 Answers

Rigidbody Addforce cancels out Rigidbody velocity..maybe? 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