- Home /
Question by
perf401 · Sep 26, 2021 at 08:38 PM ·
unity 2dif statementontriggerenter2dreturn value
Get bool value from OnTriggerEnter2D?
2 weeks of unity and C# experience. Barely knows anything.
My goal: Player can dash only once after collecting specific item.
Doesn't work because canDash is always false. Doesn't give me any errors. Looks like i just can't get value out of OnTriggerEnter2D. Maybe there is other ways to set it up?
Playermovement script:
if (Input.GetButtonDown("Fire1") && isDashing == false && Orange.GetComponent<ItemCollector>().canDash == true)
{
// dash physics
isDashing = true;
dashDurationStart = dashDuration;
}
if (dashDurationStart <= 0 && isDashing == true)
{
// dash physics
isDashing = false;
Orange.GetComponent<ItemCollector>().canDash = false;
}
ItemCollector script:
[SerializeField] public bool canDash = false;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Orange"))
{
canDash = true;
collectSound.Play();
}
}
Comment
Answer by perf401 · Sep 27, 2021 at 09:32 AM
Referencing canDash in that way fixed the problem.
dashAvailable = ItemCollector.canDash;
if (Input.GetButtonDown("Fire1") && isDashing == false && dashAvailable == true)
{
// dash physics
isDashing = true;
dashDurationStart = dashDuration;
}
if (dashDurationStart <= 0 && isDashing == true)
{
// dash physics
isDashing = false;
ItemCollector.canDash = false;
}