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 Great Alexander · Jan 17, 2013 at 11:05 AM · c#charactercontrollermove

The character controller return null refrence

I try to make my character move by using CharacterController but the problem is in the run time it tell me that the character controller does not match with any anything and the character do not move at all, so is there any falt in the code:

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Move {
     
 
 
     Transform transform;
     float gravity  = 9.8f;
     float  speed  = 3f;
      float jumpSpeed  = 0.3f;
     Animation animation;
     CharacterController controller = new CharacterController();
     Vector3 moveDirection = Vector3.zero;
 
     
      public    void Moving() {
         
         controller = (CharacterController)controller.GetComponent(typeof(CharacterController));
     
     
         transform.Rotate(0, 2 * Input.GetAxis("Mouse X"),0);
     
         if(controller.isGrounded){
     
             moveDirection.x = 0;
             moveDirection.z = 0;
         
     
             if(Input.GetKey("up")){
             
 
                 moveDirection = transform.TransformDirection(Vector3.forward);    
                 controller.Move(moveDirection * Time.deltaTime * speed);
                 animation.Play("walk");
             
             }
         
             if(Input.GetKey(KeyCode.Space)){
                 moveDirection = transform.TransformDirection(Vector3.forward);    
                 controller.Move(moveDirection * Time.deltaTime * speed);
                 animation.Play("run");
             }
         
             if(Input.GetKeyUp(KeyCode.Space)){
         
                 animation.Play("idle");
             }
         
             if(Input.GetKey("down")){
                 moveDirection = -transform.TransformDirection(Vector3.forward)* 2;    
                 controller.Move(moveDirection * Time.deltaTime * speed );
                 animation.Play("walk");
 
             }
         
         }
     
 
         moveDirection.y -= gravity*Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
 
         }
     }

 
Comment
Add comment · Show 2
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 Doireth · Jan 17, 2013 at 05:56 PM 0
Share

Are you sure that the character controller is attached to the same object that your script is attached to? GetComponent only looks at the object it is declared on. GetComponentInChildren looks at it's own object and all child objects looking for a match.

http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentInChildren.html?from=Transform

avatar image Great Alexander · Jan 17, 2013 at 08:13 PM 0
Share

Yes it is attached to the same object

2 Replies

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

Answer by Doireth · Jan 17, 2013 at 08:19 PM

The line

 controller = (CharacterController)controller.GetComponent(typeof(CharacterController));

Is getting the character controller component of the controller object, which doesn't make sense. Try instead

 controller = (CharacterController)transform.GetComponent(typeof(CharacterController));
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 Great Alexander · Jan 17, 2013 at 08:38 PM 0
Share

Thanks man it works properly now

avatar image
0

Answer by Loius · Jan 17, 2013 at 06:09 PM

You can't create new components (you can't ever do "new CharacterController", or any other type of Component). You have to use AddComponent.

Unless you're changing things into characters on the fly, you'll want to add the CharacterController component in the editor, not through script. Then your code will work (if you take out the new line, and have the controller on the same object as this script).

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 Great Alexander · Jan 17, 2013 at 08:14 PM 0
Share

I already removed GetComponent part and added this line:

     controller = (CharacterController)gameObject.AddComponent(typeof(CharacterController));


and still have the same problem

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

12 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

Related Questions

Distribute terrain in zones 3 Answers

‘CharacterController’ does not contain a definition for ‘Move’ 0 Answers

Move in global/world coordinate 1 Answer

Multiple Cars not working 1 Answer

Does moving a charactercontroller via it's transform hurt performance? 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