Im making a 2D player movement script and im trying to destroy a gameObject on collision, but im getting an error when checking if a bool is true, this is the error "Assets\PlayerMovement.cs(93,20): error CS0120: An object reference is required for the non-static field, method, or property 'PlayerMovement.isDash'" This is my code:
using System.Collections;
using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed;
public float jumpHeight;
public Rigidbody2D rb;
public bool isGrounded;
public float dashTime;
public float dashSpeed;
public float ogSpeed;
public bool isDash;
// Start is called before the first frame update
void Start()
{
ogSpeed = speed;
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if(isGrounded == true)
{
Jump();
}
}
if (Input.GetKey(KeyCode.A))
{
Left();
}
if (Input.GetKey(KeyCode.D))
{
Right();
}
if(Input.GetKey(KeyCode.Q))
{
StartCoroutine(Dash());
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void Jump()
{
isGrounded = false;
rb.velocity = new Vector2(0, jumpHeight);
}
void Left()
{
rb.velocity = new Vector2(-speed, 0);
}
void Right()
{
rb.velocity = new Vector2(speed, 0);
}
IEnumerator Dash()
{
isDash = true;
speed = dashSpeed;
yield return new WaitForSeconds(dashTime);
speed = ogSpeed;
isDash = false;
}
public class BreakCollide : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Breakable")
{
if(isDash == true)
{
Destroy(collision.collider.gameObject);
}
}
}
}
}
Thanks!