Reputation: 1
My problem is two-fold. Animation and a GameObject jumping off during Play. I don't know the exact phrases of what happens, I'll try to explain best I can.
I want to increase a float upon movement, which should trigger animation for my character. It is the SpeedBlend in image below. It's between 0 and 1.
The movement works fine and debugging works fine. But I cannot get the SpeedBlend to increase.
Here are the issues:
It says "NullReferenceException: Object reference not set to an instance of an object" when I press Play. I narrowed it down to the GameObject jumping off the public Animator when i press Play. I don't know the exact wording of what happens, but the "Animation (Animator)" is no longer attached to the Scripts' Animator. Does that make sense? See below.
The "Animation (Animator)" jumps off on Play. If I declare the animator in Start function, it jumps off. If I don't declare it, it stays on during Play. See below.
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
controller = GetComponent<CharacterController>();
animator = GetComponentInChildren<Animator>();
}
If I re-apply the "Animation (Animator)" to the Scripts' Animator during Play, it changes the problem to "NotImplementedException: The method or operation is not implemented."
My current solution for animating my character is:
//animation
if (direction != Vector3.zero)
{
animator.SetFloat("SpeedBlend", 0.5f);
Debug.Log("moving works fine");
}
The debugging works fine. So my questions are two-fold:
animator.SetFloat("SpeedBlend", 0.5f);
doesn't work. Why not?Just in case you want the read the entire code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
//moving
public float speed = 6f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
//https://www.youtube.com/watch?v=_QajrabyTJc&ab_channel=Brackeys
//gravity + jumping
public float gravity = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float jumpHeight = 3f;
Vector3 velocity;
bool isGrounded;
//Animation
public Animator animator;
// Start is called before the first frame update
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
controller = GetComponent<CharacterController>();
animator = GetComponentInChildren<Animator>();
}
// Update is called once per frame
void Update()
{
Move();
/*
Idle();
*/
Run();
}
void Move()
{
//isgrounded
isGrounded = Physics.CheckSphere(transform.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//moving or walking
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDirection.normalized * speed * Time.deltaTime);
}
//gravity
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//jumping
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
//animation
if (direction != Vector3.zero)
{
animator.SetFloat("SpeedBlend", 0.5f);
Debug.Log("moving works fine");
}
}
/*
void Idle()
{
animator.SetFloat("SpeedBlend", 0);
}
*/
void Run()
{
//running
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 12f;
Debug.Log("left shift presseed");
/*
animator.SetFloat("SpeedBlend", 1f);
*/
}
else
{
speed = 6f;
}
}
}
I tried changing movement scripts, changing animation types. Instead of the current Blend Tree, I tried using a bool parameter "isWalking". None of it works, and I can't for the love of me grasp why. If I manually increase the SpeedBlend during Play, the animation changes as it should.
Upvotes: 0
Views: 3931
Reputation: 1189
If you cannot run your app in debug mode (which would show you the exception and its StackTrace
):
Add a try-catch
to your code
try
{
// the code that throws
}
catch (Exception ex)
{
// Set a break point or log the exception and its StackTrace.
Console.WriteLine(ex.ToString());
}
Try to log the StackTrace
for example to a file or to the console (if you have no logging set up). That will show you what method throws the exception.
You can also get the whole exception as a string, with inner exception and all StackTraces via ex.ToString()
and log that (instead of the StackTrace property only). See the code example above.
In case of the NotImplementedException
: probably a base class has a virtual
or abstract
method that that you need to override
.
Upvotes: 0