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 /
  • Help Room /
This question was closed Jan 21, 2016 at 08:29 AM by Veneryn for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Veneryn · Jan 21, 2016 at 08:10 AM · charactercontrolleraddforceobject-reference-error

Character controller adding force to an object (soccerball) - working fine but getting error messages

Hello. I'm making a simple platformer game similar to the early spyro games. I'm using a character controller, and in my script I added a part that will be used to push objects by using addforce. Everything is working perfectly, but I'm getting this error message:

NullReferenceException: Object reference not set to an instance of an object tryagain.OnControllerColliderHit (UnityEngine.ControllerColliderHit hit) (at Assets/tryagain.cs:44) UnityEngine.CharacterController:Move(Vector3) tryagain:Update() (at Assets/tryagain.cs:35)

Here is my code on the player. Ignore the massive list of trigger events, I don't know how to organize it better :(.

using UnityEngine; using System.Collections;

public class tryagain : MonoBehaviour { public float moveSpeed = 30.0f; public float rotateSpeed = 200.0f; public float jumpSpeed = 15.0f; public float gravity = 30f; public float pushPower2 = 50.0F; Vector3 currentMovement; public CharacterController controller; public GameObject spawn;

 void Start ()
 {

 }

 void Update ()
 {

     transform.Rotate (0, Input.GetAxis ("Horizontal") * rotateSpeed * Time.deltaTime, 0);

     currentMovement = new Vector3 (0, currentMovement.y, Input.GetAxis ("Vertical") * moveSpeed);
     currentMovement = transform.rotation * currentMovement;

     if (!controller.isGrounded)
         currentMovement -= new Vector3 (0, gravity * Time.deltaTime, 0);
     else
         currentMovement.y = 0;
     if (controller.isGrounded && Input.GetButton ("Jump"))
                     currentMovement.y = jumpSpeed;

     controller.Move (currentMovement * Time.deltaTime);
     if (Input.GetKey(KeyCode.R))
         transform.position = spawn.transform.position;

 }

 void OnControllerColliderHit(ControllerColliderHit hit)
 {
     Rigidbody body = hit.collider.attachedRigidbody;
     body.AddForce (transform.forward * pushPower2);
 }

 void OnTriggerEnter(Collider other) 
 {
     //if (other.gameObject.CompareTag ("betalevel2"))
     //{
     //    Application.LoadLevel ("betalevel2"); 
     //}
     if (other.gameObject.CompareTag ("PickUp"))
     {
         other.gameObject.SetActive (false);
     }
     if (other.gameObject.CompareTag ("betalevel2"))
     {
         Application.LoadLevel ("betalevel2"); 
     }
     if (other.gameObject.CompareTag ("level 1"))
     {
         Application.LoadLevel ("level 1");
     }
     if (other.gameObject.CompareTag ("winlevel"))
     {
         Application.LoadLevel ("winlevel"); 
     }
     if (other.gameObject.CompareTag ("bouncy"))
     {
         jumpSpeed = 75.0f;
     }
     if (other.gameObject.CompareTag ("bouncy2"))
     {
         jumpSpeed = 200.0f;
     }
     if (other.gameObject.CompareTag ("level 1 prologue"))
     {
         Application.LoadLevel ("level 1 prologue"); 
     }
     if (other.gameObject.CompareTag ("cooljump"))
     {
         Application.LoadLevel ("cooljump"); 
     }
 }

}

The part that adds force is the OnControllerColliderHit function. I'm confused because other answers on this site say that the error message means the object doesn't exist. The way I understand the code, the controller stores information of the object that was hit in the "hit" variable, and the rigidbody of the hit object is referenced and the force is added to it. I don't know what's wrong with it! :(

Like I said, it's working exactly how I want it, but the console is SPAMMING me with error messages to the point where it's lagging the play simulation in the editor. I'm quite new to unity and I'm using this as a big learning project so help on this problem is appreciated! Any other advice on my script is welcome too. Thank you!

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 Veneryn · Jan 21, 2016 at 08:12 AM 0
Share

Looks like the paste of my code got messed up a bit with the numbers. The line the error message refers to is line 30 in my paste (the one with body.addforce).

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Veneryn · Jan 21, 2016 at 08:29 AM

Just figured out what was wrong! I was missing four handy lines that I had to grab from another example. For those of you with the same problem, here is what I changed the controllercollider function to:

void OnControllerColliderHit(ControllerColliderHit hit) { Rigidbody body = hit.collider.attachedRigidbody; if (body == null || body.isKinematic) return; if (hit.moveDirection.y < -0.3) return; body.AddForce (transform.forward * pushPower2); }

The error message earlier stated that something didn't exist; what was happening was that my controller was colliding with the scenery which doesn't have rigidbodies. The hit variable tried to reference those non-existant rigidbodies, hence the error message. I hope this helped anyone else with the same problem! I'm closing the question now.

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

Follow this Question

Answers Answers and Comments

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

Related Questions

Using "AddForce" with a Character Controller 1 Answer

need help badly 2 Answers

AddForce in Character Controller problem 0 Answers

Character Controller to create a lateral dash (1st person) - Keep Momentum by Adding Force? 0 Answers

How to create smooth jump with rigidbody.addforce? 2 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