0
\$\begingroup\$

When I run this script in Unity: (ignore the clones variable)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Throw : MonoBehaviour
{
    public Rigidbody rb;

    private float clones = 0;

    public KeyCode throwKey = KeyCode.Mouse0;
    private void Update()
    {
        if (transform.parent != null)
        {
            gameObject.name = "Wood";
            rb.isKinematic = true;
            if (Input.GetKeyDown(throwKey))
            {
                Vector3 position = new Vector3(transform.parent.position.x, transform.parent.position.y, transform.parent.position.z);
                Instantiate(gameObject, position, Quaternion.Euler(0, 0, 0));

                rb.AddForce(transform.forward * 500f, ForceMode.Impulse);
                transform.SetParent(null);
                StartCoroutine(Wait());
            }
        }
        else
        {
            gameObject.name = "thrownWood";
            rb.isKinematic = false;
        }
    }

    IEnumerator Wait()
    {
        yield return new WaitUntil(Input.GetKeyUp(throwKey));
    }
}

It comes up with the error message:

(Assets/Throw.cs(37,36): error CS1503: Argument 1: cannot convert from 'bool' to 'System.Func')

Any idea for why this happens?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

When you follow a function with open and close parentheses, you are calling that function and taking its return value as the result of the expression.

So here, where you meant to pass a reference to the function, you instead called the function once, got a single bool value (true or false), and tried to pass that bool value as a waiting condition when a function (a rule for choosing a bool) was needed.

You can fix it like so:

bool WaitCondition() {
    return Input.GetKeyUp(throwKey);
}

IEnumerator Wait() {
    yield return new WaitUntil(WaitCondition);
}

Note that there's no () after WaitCondition when we pass it to WaitUntil — we're referring to the function, not calling/invoking it to get its return value.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .