- Home /
Controls are not cooperating with me all of a sudden.
I've been working on my project and trying to make the animation and coding work. I added the animations but now all of a sudden, the movement in the player script is all broken and not working.
This is my only script, so there's no other ones I need to publish, if you'd like to see my Animator or anything else like that, let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
//General Variables
public float speed;
private Rigidbody2D rb;
private GameObject sword;
//Class Setting Variables
public PlayerClassTypes eCurClass = PlayerClassTypes.Tank;
//Animator Variables
Animator sword_Anim;
void Start ()
{
//Sets variables automatically on launch
sword_Anim = sword.GetComponent<Animator> ();
sword = GameObject.Find ("Sword");
rb = gameObject.GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
//Making the velocity variables to make 'fake friction'
Vector3 easeVelocity = rb.velocity;
easeVelocity.y *= 0.75f;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.75f;
//Sets the variables to move them up and down
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
//Fake Friction / Easing the x speed of our player
rb.velocity = easeVelocity;
//Moving the player up and down
rb.AddForce (Vector2.up * speed * v);
rb.AddForce (Vector2.right * speed * h);
}
//Sets the Player classes
public enum PlayerClassTypes
{
Tank,
DPS,
Healer
}
//Handles the Tank class
void HandleTankState ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
//Starts Sword Swing animation
sword_Anim.SetBool("swing_Animation", true);
}
}
}
The error message I'm getting is "Object reference not set to an instance of an object Player.FixedUpdate () (at Assets/Scripts/Player.cs:30)"
Thanks for looking over my script and any input at all would be very helpful!
Check to see if you accidentally unchecked the 2D RigidBody component on your Player gameObject.
I checked the Rigidbody 2D component and it's still enabled
Answer by sandermanderthefirst · Jan 19, 2018 at 09:34 PM
I found out what was wrong. I didn't reference the sword variable correctly and ended up going a different method to calling it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150724im_/https://answers.unity.com/themes/thub/images/avi.jpg)