Click here to Skip to main content
15,030,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to programming on c#, I have been watching a 3 and a half hour tutorial and up to this point everything was going smooth. The error message says "There is no argument given that corresponds to the required formal parameter 'force' of 'ForceReceiver.AddForce(Vector3, float)' ". I am aware that it is a common problem but the other answers online have not helped me. I do not understand what has happened incorrectly with the script, here is my code.
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ForceReceiver : MonoBehaviour{
    public float deceleration = 5;
    public float mass = 3;

    private Vector3 intensity;
    private CharacterController character;


    // Start is called before the first frame update
    void Start()
    {
        intensity = Vector3.zero;
        character = GetComponent<charactercontroller> ();
    }

    // Update is called once per frame
    void Update()
    {
        if (intensity.magnitude > 0.2f) {
            character.Move (intensity * Time.deltaTime);
        }
        intensity = Vector3.Lerp (intensity, Vector3.zero, deceleration * Time.deltaTime);
    }
    public void AddForce (Vector3 direction, float force ) {
        intensity +=  direction.normalized * force / mass;
    }
}

This code is for a knockback affect for damage in my game which is connected to this player script...
C#
//Perform the knockback effect. 
            } else if (otherCollider.GetComponent<bullet>() != null) {
                Bullet bullet = otherCollider.GetComponent<bullet> ();
                if (bullet.ShotByPlayer == false) {
                    hazard = bullet.gameObject;

                    health -= bullet.damage;
                }
            }

            if (hazard != null) { 
                isHurt = true;

                 Vector3 hurtDirection = (transform.position - hazard.transform.position).normalized;
            Vector3 knockbackDirection = (hurtDirection = Vector3.up).normalized;
            GetComponent<forcereceiver>().AddForce (knockbackDirection * knockbackForce);


            StartCoroutine (HurtRoutine ()); 
            }


The Unity error message says its on this line
C#
GetComponent<forcereceiver>().AddForce (knockbackDirection * knockbackForce);

I am completely clueless on what the problem is. If you know, your help would be much appreciated. If you need any more of my scripts or examples to find out what the problem is just tell me. Thank you.

What I have tried:

I looked every where for the answer, but being new to programming I am not sure what the problem is. I made sure my code was the same as the tutorial numerous times, and on the same version of unity.
Posted
Updated 7-Jan-21 22:45pm
v3

ForceReceiver.AddForce(Vector3, float)

The above says .AddForce takes TWO parameters; you're only passing ONE.

It needs a vector and a float; probably "," instead of "*".
   
Comments
Blake Burnell 8-Jan-21 14:03pm
   
Thank you that worked, accept the knockback is a little bit scuffed so I'm gonna tweak with some of the variables and try to fix it. Thank you.👍
Quote:
I have been watching a 3 and a half hour tutorial
Probably a waste of time. Download a copy of .NET Book Zero by Charles Petzold[^], which is an excellent learning resource.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900