Hi guys!
It’s been a while since I’ve gotten back into Unity, and I’m trying to make a game to make my player (a squirrel) move, but I’m getting an error saying “Object reference not set to an instance of an object”. Nothing looks wrong to me with the code or my Player object (which has a Rigidbody2D component). Here’s my code:
public float maxSpeed = 10f;
bool facingRight = true;
public GameObject playerobject;
private Rigidbody2D squirrel;
void Start() {
}
void FixedUpdate() {
float move= Input.GetAxis("Horizontal");
squirrel.velocity = new Vector2(move *maxSpeed, squirrel.velocity.y);
if (move > 0 && !facingRight) {
Flip ();
} else if (move < 0 && facingRight) {
Flip ();
}
}
void Flip() {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
EDIT: The compiler is saying the error is on line 12
Because of that one error, I can’t get my player to move. My player is also in space so there’s 0 gravity for the gravity scale, but I don’t think that’s causing the player not to move.
Also, could anyone recommend a tutorial that would help me implement a highscore/leaderboard to my game as well as a pause function?
Thanks in advance!