Hey! so i’m trying to make it so that when my character falls in the void or touches spikes (both have the tag “FallDetection”) that the sprite moves back to it’s original position. but it doesn’t seem to work. I get the errors: “Object reference not set to an instance of an object” & “the name ‘PlayerSprite’ does not exist in the current context”
This is my playermovement code, which holds the death stuff too.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour {
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
// Update is called once per frame
void Update () {
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (Input.GetButton("Jump"))
{
jump = true;
animator.SetBool("IsJumping", true);
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
animator.SetBool("IsJumping", false);
} else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
//is called when object lands
public void OnLanding ()
{
animator.SetBool("IsJumping", false);
}
//is called when object uses crouching input
public void OnCrouching (bool isCrouching)
{
animator.SetBool("IsCrouching", isCrouching);
}
void FixedUpdate ()
{
// Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if ( collision.tag == "FallDetection") {
PlayerSprite.transform.position = new Vector3(1, 0, -10);
}
if ( collision.tag == "Finish") {
SceneManager.LoadScene ("LevelMenu");
}
}
}