- Home /
Can't move character. C#
Hi there guys, I have been following along with a few tutorials and the most recent one that I have been trying to complete is the stealth game tutorial. I found my self stuck at the "character movement" section. I copied and pasted the exact text from the video, but for some reason I can't move my avatar like the person does in the video. I have about 15 errors but here are the only two that pertain to the character.
"NullReferenceException: Object reference not set to an instance of an object PlayerHealth.Awake () (at Assets/Scripts/PlayerHealth.cs:25)"
"NullReferenceException: Object reference not set to an instance of an object PlayerMovement.Awake () (at Assets/Scripts/Player/PlayerMovement.cs:19)"
Also here is the movement code.
using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour { public AudioClip shoutingClip; // Audio clip of the player shouting. public float turnSmoothing = 15f; // A smoothing value for turning the player. public float speedDampTime = 0.1f; // The damping for the speed parameter
private Animator anim; // Reference to the animator component.
private HashIDs hash; // Reference to the HashIDs.
void Awake ()
{
// Setting up the references.
anim = GetComponent<Animator>();
hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
// Set the weight of the shouting layer to 1.
anim.SetLayerWeight(1, 1f);
}
void FixedUpdate ()
{
// Cache the inputs.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool sneak = Input.GetButton("Sneak");
MovementManagement(h, v, sneak);
}
void Update ()
{
// Cache the attention attracting input.
bool shout = Input.GetButtonDown("Attract");
// Set the animator shouting parameter.
anim.SetBool(hash.shoutingBool, shout);
AudioManagement(shout);
}
void MovementManagement (float horizontal, float vertical, bool sneaking)
{
// Set the sneaking parameter to the sneak input.
anim.SetBool(hash.sneakingBool, sneaking);
// If there is some axis input...
if(horizontal != 0f || vertical != 0f)
{
// ... set the players rotation and set the speed parameter to 5.5f.
Rotating(horizontal, vertical);
anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
}
else
// Otherwise set the speed parameter to 0.
anim.SetFloat(hash.speedFloat, 0);
}
void Rotating (float horizontal, float vertical)
{
// Create a new vector of the horizontal and vertical inputs.
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
// Create a rotation based on this new vector assuming that up is the global y axis.
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
// Create a rotation that is an increment closer to the target rotation from the player's rotation.
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
// Change the players rotation to this new rotation.
rigidbody.MoveRotation(newRotation);
}
void AudioManagement (bool shout)
{
// If the player is currently in the run state...
if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
{
// ... and if the footsteps are not playing...
if(!audio.isPlaying)
// ... play them.
audio.Play();
}
else
// Otherwise stop the footsteps.
audio.Stop();
// If the shout input has been pressed...
if(shout)
// ... play the shouting clip where we are.
AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
}
}
I'd love some help with this, thanks in advance.
Answer by zeroblackstar · Oct 01, 2014 at 08:14 PM
Hi Xeroux, the code given with the Unity tutorials seem to be buggy, for instance I've noticed a few missing curly braces "{" and semi-colons ";".
I would read through and check your basics like ensuring statements finish with a ";" and your functions/methods have correct braces "{}", also carefully check your variable names etc
I was also running into a null pointer exception and after triple checking the code I found that just restarting Unity fixed the problem! Not sure what was going on there but did the trick for me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144841im_/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
changing animations on an object without the script. 0 Answers
Help with character movement and camera 1 Answer
[C#] Unity 2D: Changing animation when player is in motion. 1 Answer