• 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 TheDemin · Feb 23, 2014 at 08:03 PM · c#nullreferenceexception

NullRefernceError in WalkingSound

This is a program that plays two different sound files depending on whether the Player is 'running' (Holding down left or right shift), or walking. The problem is, I get an error when I run it that states there is a NullReferenceError at lines 34, and 55, though I suspect that line 34 is just pointing to line 55.

After looking through the code of CharacterMotor, I know that all the variables are initialized with a value, so I am lost. Also, I have read the several guides on the NullReferenceError that people have posted on other questions, but I am an amateur programmer, and do not possess the capabilities of implementing the answers correctly. Any ideas? Thank you in advance.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioSource))]
 
 public class WalkingSoundScript : MonoBehaviour {
 
 
  
 AudioClip walk;
 AudioClip run;
  
 float walkAudioSpeed = 0.4f;
 float runAudioSpeed = 0.2f;
  
 private float walkAudioTimer = 0.0f;
 private float runAudioTimer = 0.0f;
  
 bool  isWalking = false;
 bool  isRunning = false;
  
 float walkSpeed = 7; // regular speed
 float runSpeed = 20; // run speed
  
 private CharacterController chCtrl;
 private CharacterMotor chMotor;
  
 void  Start (){
     chCtrl = GetComponent<CharacterController>();
     chMotor = GetComponent<CharacterMotor>();
 }
  
 void  Update (){
     SetSpeed();
  
     if ( chCtrl.isGrounded )
     {
         PlayFootsteps();
     }
     else
     {
         walkAudioTimer = 0.0f;
         runAudioTimer = 0.0f;
     }
 }
  
 void  SetSpeed (){
     float speed= walkSpeed;
  
     if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
     {
         speed = runSpeed;
     }
  
     chMotor.movement.maxForwardSpeed = speed;//NULL REFERENCE ERROR here.
 }

 
Comment
Add comment · Show 8
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 getyour411 · Feb 23, 2014 at 08:35 PM 0
Share

Remove movement from ch$$anonymous$$otor.movement.maxForwardSpeed

 ch$$anonymous$$otor.maxForwardSpeed =  someVal
avatar image TheDemin getyour411 · Feb 23, 2014 at 08:43 PM 0
Share

Did that, got this as an error message: "Assets/WalkingSoundScript.cs(55,13): error CS1061: Type Character$$anonymous$$otor' does not contain a definition for maxForwardSpeed' and no extension method maxForwardSpeed' of type Character$$anonymous$$otor' could be found (are you missing a using directive or an assembly reference?)"

avatar image getyour411 getyour411 · Feb 23, 2014 at 08:45 PM 0
Share

Open your Character$$anonymous$$otor and make sure it's the same as the one I'm looking at. In $$anonymous$$e/default one, one of the first variables is maxForwardSpeed - confirm it's same for yours.

Assu$$anonymous$$g it is, your issue might be accessing JS & JS variables from C#

avatar image TheDemin getyour411 · Feb 23, 2014 at 09:16 PM 0
Share

First of all, I believe I am accessing JS variables from C# (Teacher wanted us to do it in c#, don't know why, but that's another story.)

As for the variable, the first variable in the class Character$$anonymous$$otor$$anonymous$$ovement is indeed maxForwardSpeed, but the first variable in the overall class (Character$$anonymous$$otor) is canControl. I will be happy to send you a copy of the 'Character$$anonymous$$otor' script that I am looking at if you are confused, and thanks for your help.

Show more comments
avatar image TheDemin · Feb 23, 2014 at 11:49 PM 0
Share

Well, thanks anyway for all your help.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Linus · Feb 24, 2014 at 12:03 AM

If CharacterMotor is UnityScript, it needs to be in a folder called Standard Assets or Plugins. To be available in C#

See https://docs.unity3d.com/Documentation/Manual/ScriptCompileOrderFolders.html

Comment
Add comment · Show 3 · 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 TheDemin · Feb 25, 2014 at 03:36 AM 0
Share

The problem is now I am getting an error that says:

Assets/Standard Assets/WalkingSoundScript.cs(26,9): error CS0246: The type or namespace name `Character$$anonymous$$otor' could not be found. Are you missing a using directive or an assembly reference?

avatar image Linus · Feb 25, 2014 at 04:08 AM 0
Share

Try not having WalkingSoundScript.cs in a special folder, but have Character$$anonymous$$otor.js in Plugins or Standard Assets folder. I prefer Plugins, because that leaves one folder up in the chain if needed.

avatar image TheDemin · Feb 25, 2014 at 04:29 PM 0
Share

Came up with this error:

NullReferenceException: Object reference not set to an instance of an object WalkingSoundScript.SetSpeed () (at Assets/WalkingSoundScript.cs:55) WalkingSoundScript.Update () (at Assets/WalkingSoundScript.cs:34)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can't load Mouse Cursor texture 1 Answer

RaycastHit2D not interacting with tags 1 Answer

NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 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