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 Euld · Dec 26, 2012 at 02:30 AM · c#error messagescriptingbasicsobject reference

C#, "object reference not set to an instance of an object"

Using the CharacterController.Move script from the Unity Script Reference site, and I get an error that says "object reference not set to an instance of an object." I really have no idea what I'm doing wrong. Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class Script_PlayerController : MonoBehaviour {
     
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     
     void Update() {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
Comment
Add comment · Show 5
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 Dave-Carlile · Dec 26, 2012 at 03:18 AM 0
Share

It would be helpful if we knew which line the error is on.

avatar image Euld · Dec 26, 2012 at 03:41 AM 0
Share

Oops sorry, the error is on this line:

CharacterController controller = GetComponent#CharacterController#();

replace the hashtags with < and >. Sorry about the multiple edits within the same $$anonymous$$ute, it kept deleting anything I posted within the < and >.

avatar image Imankit · Dec 26, 2012 at 04:58 AM 0
Share

Cut and paste controller=GetComponent(); in void Start()

You don't need to Get the component in every Update and declare the CharacterController controller globaly

avatar image Euld · Dec 26, 2012 at 07:10 AM 0
Share

$$anonymous$$oving that line of code to anywhere but Update gives me errors. I agree, declaring it every update definitely sounds like a waste, but I get errors if goes anywhere else.

Also, not sure how to declare CharacterController controller globally. Adding a "public" before it gives me more errors, which I thought was the way to declare global variables 'n such?

avatar image rangapraveen · Aug 15, 2013 at 10:29 AM 0
Share

You can also declare Character Controller Globally, like .... [RequiredComponent(typeof(CharacterController))] it shuld be declare before the class..

3 Replies

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

Answer by Euld · Dec 27, 2012 at 07:49 AM

Ok I think I've got it. The script is trying to access another script or component called Character Controller. It's possible to add this component through the menu by the way of Component -> Physics -> Character Controller. My Player was falling through the map for a while, but all I had to do was raise the Player up a bit.

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
0

Answer by anshul-bhardwaj · Dec 26, 2012 at 08:33 AM

use

 CharacterController controller = GetComponent<CharacterController>() as CharacterController;

instead of :

CharacterController controller = GetComponent#CharacterController#();

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 Dave-Carlile · Dec 26, 2012 at 03:17 PM 0
Share

The as is not necessary since the data type is already known because of the use of generics.

avatar image Euld · Dec 26, 2012 at 10:42 PM 0
Share

Nice try, but I'm still getting the same error as before. Removing the "as" gives me an error in $$anonymous$$onoDevelop.

Is there something I'm missing? Like should the object this script is attached to be named a certain way, or shouldn't have any children parented to it?

avatar image
0

Answer by SirAlwayne · Feb 18, 2013 at 01:21 PM

CharacterController controller = gameObject.GetComponent();

that should fix ya

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why am I getting this error ? 2 Answers

Get Boolean from another Script, C# 2 Answers

Help! How to change Real numbers to Natural numbers on Distance Counter script (Noob question) 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