-1

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!

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
brix
  • 1
  • 1
  • MonoBehaviours should each be in their own file, named exactly the same as the class. Nesting MonoBehaviours in the way shown above is not a good idea. To get access to the isDash field, first get an instance of the PlayerMovement component. If these scripts are on the same object, you can store GetComponent in a global variable from Awake or Start, then whenever you need to check the isDash, refer to the instance variable. – hijinxbassist Jul 28 '21 at 18:12
  • Thanks sm, but i managed to figure this out on my own after about 5 minutes of reading back over this, and i now know how stupid i am for copy and pasting the class from my other script – brix Jul 28 '21 at 19:34
  • Do not add words like "solved", "updated", etc. to the title of posts. The site already has other mechanisms to convey this information. – Peter Duniho Jul 30 '21 at 02:56

1 Answers1

0

Like hijinxbassist said. You cannot store 2 MonoBehaviour classes in 1 file. Always create a new file for different MonoBehaviour scripts, and make sure the class is named exactly as the file. What you can do is get the instance of that script in your other script, and access its variables like this.

class PlayerController : MonoBehaviour {
    //variables
}

And your other script:

class OtherScrip : MonoBehaviour {
    PlayerController playerController;
    
    private void Start() {
        //this only works if the scripts are on the same GameObject
        playerController = GetComponent<PlayerController>();
        
        //now access variables in playerController using:
        playerController.x = y;
    }
}
TheMatrixAgent22
  • 83
  • 1
  • 1
  • 8