I the line “GetComponents().addforce (Vector2.right * power);” there is an error saying
“Type ‘UnityEngine.Riidbody2D’ does not contain a definition for ‘addforce’and no extension method ’ addforce’ of type 'unityEngine.Rigidbody2D could not be found. Are you missing an assembly refernce?” How do I fix it? @username
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public int power = 500;
public int jumpHeight = 100;
public bool isFalling = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
GetComponents<Rigidbody2D>().addforce (Vector2.right * power);
}
}
There is GetComponent(Singular) and GetComponents(Plural). When you use the plural version (GetComponents) the method/function returns an array of whatever type you specify you’re looking for. In this case RigidBody. It seems like you want the singular version which will return a single Rigidbody2D if available on the GameObject.
Change:
GetComponents().addforce (Vector2.right * power);
to
GetComponent().AddForce (Vector2.right * power);
Also @Davirtuoso was correct, you need to specify the methods with exact case.
I think the issue is your need to capitalise letters within ‘addforce’, like so:
GetComponent<Rigidbody2D>().AddForce (Vector2.right * power);
Captilisation matters a lot in c# and ‘addforce’ is considered a completely different method than ‘AddForce’. I hope this helps