"MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it"
Hi, so i am getting the error "MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it"
I know that it is because im only destroying part of my character when a game object hits it, so how would i fix said error?
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movement : MonoBehaviour { public GameObject leftLeg; public GameObject rightLeg; Rigidbody2D leftLegRB; Rigidbody2D rightLegRB; public Rigidbody2D rb; public Animator anim; [SerializeField] float speed = 1.5f; [SerializeField] float legStepWait = .5f; [SerializeField] float jumpForce = 10; private bool isOnGround; public float positionRadius; public LayerMask ground; public Transform playerPos; // Start is called before the first frame update void Start() { leftLegRB = leftLeg.GetComponent<Rigidbody2D>(); rightLegRB = rightLeg.GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { if (Input.GetAxisRaw("Horizontal") != 0) { if (Input.GetAxisRaw("Horizontal") > 0) { anim.Play("WalkRight"); StartCoroutine(MoveRight(legStepWait)); } else { anim.Play("WalkLeft"); StartCoroutine(MoveLeft(legStepWait)); } } else { anim.Play("Idle"); } isOnGround = Physics2D.OverlapCircle(playerPos.position, positionRadius, ground); if(isOnGround == true && Input.GetKeyDown(KeyCode.Space)) { rb.AddForce(Vector2.up * jumpForce); } } IEnumerator MoveRight(float seconds) { leftLegRB.AddForce(Vector2.right * (speed * 1000) * Time.deltaTime); yield return new WaitForSeconds(seconds); rightLegRB.AddForce(Vector2.right * (speed * 1000) * Time.deltaTime); } IEnumerator MoveLeft(float seconds) { rightLegRB.AddForce(Vector2.left * (speed * 1000) * Time.deltaTime); yield return new WaitForSeconds(seconds); leftLegRB.AddForce(Vector2.left * (speed * 1000) * Time.deltaTime); } }
Your answer
![](https://koobas.hobune.stream/wayback/20220613054656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Make A Character Stop At Wall? 0 Answers
Preventing A Teleporting GameObject From Passing Through Walls 2 Answers
How To Make A Character Stop At A Block? 0 Answers
How to move Raycast Hit Object 0 Answers