- Home /
Type or namespace 'CharacterMotor' cannot be found
This is my RunningSound code, which uses triggers to play 'walking' and 'running' sounds. The problem is, I have no idea how to fix the CharacterMotor error I keep getting at runtime. The CharacterMotor and this file are in the same folder in the Standard Assets file, and the file below is a C# file, while the CharacterMotor is a Javascript file. Any suggestions on what I should do? Thank you in advance for your help.
using UnityEngine;
using System.Collections;
using CharacterMotor;//Unsure of this area, does not seem to do anything.
//Still here to show it does not work.
[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;//Note that CharacterMotor is a Javascript file, and is not considered an object.
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;
}
void PlayFootsteps (){
if ((Input.GetAxis( "Horizontal" )>0) || (Input.GetAxis( "Vertical" )>0) )
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
// Running
isWalking = false;
isRunning = true;
}
else
{
// Walking
isWalking = true;
isRunning = false;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
}
// Play Audio
if ( isWalking )
{
if ( audio.clip != walk )
{
audio.Stop();
audio.clip = walk;
}
//if ( !audio.isPlaying )
if ( walkAudioTimer > walkAudioSpeed )
{
audio.Stop();
audio.Play();
walkAudioTimer = 0.0f;
}
}
else if ( isRunning )
{
if ( audio.clip != run )
{
audio.Stop();
audio.clip = run;
}
//if ( !audio.isPlaying )
if ( runAudioTimer > runAudioSpeed )
{
audio.Stop();
audio.Play();
runAudioTimer = 0.0f;
}
}
else
{
audio.Stop();
}
// increment timers
walkAudioTimer += Time.deltaTime;
runAudioTimer += Time.deltaTime;
}
}
Answer by rutter · Feb 28, 2014 at 01:15 AM
Unity does allow some interaction between C# and JS files, but it's very limited. Unity compiles your scripts in several stages. One script can "see" the other if it's compiled in an earlier stage. You can find a list of compilation stages here.
If this is your only cross-language dependency, you can probably just move WalkingSoundScript out of Standard Assets.
If you have a lot of dependencies, things can get ugly. I try to stick to one language for a project.
Thankfully, it is the only dependency I have. I have moved it out of the Standard Asset folder. The problem is, it becomes a repeat of a previous error that I had before: A NullReferenceError. I did not intend on repeating this error, so I will simply link back to it to avoid repeating myself across multiple questions and wasting everyone's time. Thank you for your assistance though.
http://answers.unity3d.com/questions/648014/nullrefernceerror-in-walkingsound.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613134943im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Error the type or namespace name 1 Answer
Name GUI input help in C# 0 Answers
Converting a JS script into C# - hit a wall with arrays... 1 Answer