Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Issues regarding projectiles

Discussion in '2D' started by bjohnson22, Jan 25, 2021.

  1. bjohnson22

    bjohnson22

    Joined:
    Jan 22, 2021
    Posts:
    3
    My Editor has given me an error telling me that the function "Launch" has no definition, but it does. Could someone give me insight into why this might be happening? My code is below:

    public class ProjectileScript : MonoBehaviour
    {
    Rigidbody2D rigidbody2d;

    void Start()
    {
    rigidbody2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
    if (transform.position.magnitude > 1000.0f)
    {
    Destroy(gameObject);
    }
    }
    public void Launch(Vector2 direction, float force)
    {
    rigidbody2d.AddForce(direction * force);
    }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    How are you calling the Launch function? By UI button or Input press? Or by OnTriggerEnter, etc.?
     
  3. bjohnson22

    bjohnson22

    Joined:
    Jan 22, 2021
    Posts:
    3
    By key down in my main character script which has no issues.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    So from a brief google, it looks like it usually is a spelling error. Check where you call it and make sure the function is spelled correctly. Also, make sure you are accessing the ProjectileScript properly, once again, check spelling.

    https://answers.unity.com/questions/982337/scripting-error-no-definition-or-an-extension-meth.html

    https://answers.unity.com/questions/1142289/error-cs1061does-not-contain-a-definition-forand-n.html

    https://answers.unity.com/questions/1428936/does-not-contain-definition-1.html
     
  5. Kalliber95

    Kalliber95

    Joined:
    Mar 27, 2015
    Posts:
    19
    Can you show us the script for your main character?
     
  6. bjohnson22

    bjohnson22

    Joined:
    Jan 22, 2021
    Posts:
    3

    Edit: I have resolved the issue, Thanks!
     
    Kalliber95 likes this.