Unity Error CS0120 - unity3d

I just got into unity3d, and i was using a guide. But every time I try to compile, I get Error CS0120. Please just help me instead of marking it as duplicate.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
Rigidbody.AddForce(movement*speed*Time.deltaTime);
}
}

This line is the issue
Rigidbody.AddForce(movement*speed*Time.deltaTime);
The error you are getting is that
error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3)'
If you wanted to AddForce to a rigidbody attatched to the same gameobject as your script, then you need to grab that ridigbody instance and apply force, like this.
this.GetComponent<Rigidbody>().AddForce(movement*speed*Time.deltaTime);

Related

I'm new to Unity3D , Getting compiler errors

I am new to Unity and I'm making a stickman game I added a balance script.
But it shows a compiler error "The variable of rb has not been assigned".
Can u please tell me how to assign a variable?
Here's the code-
// Update is called once per frame
void Update()
{
rb.MoveRotation(Mathf.LerpAngle(rb.rotation, targetRotation, force * Time.fixedDeltaTime));
}
Because you didn't to use a Rigidbody you need to get the component from the GameObject and in your case make the actual Rigidbody variable you want to use.
Your code should look like that:
using UnityEngine;
public class Example : MonoBehaviour
{
Rigidbody rb;
void Start()
{
//Fetch the Rigidbody from the GameObject with this script attached
rb= GetComponent<Rigidbody>();
}
void Update()
{
rb.MoveRotation(.....);
}
}

Struggling to get my animations to work on my first game

I am trying to make my first game and have been struggling to get my animations to work.
I have been following along with a YouTube course and when it came to coding the animations on Unity i cannot seem to get it to work.
Please can someone who understands coding and Unity have a look at the following screenshots and help me out.
This is my code for my game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
private Vector3 input;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Verical"));
Rigidbody.AddForce(input * moveSpeed);
}
}
I cannot get the white dot next to my Player Control script to light up and it will not work.
Any help is appreciated, thanks.
If you are able to change the script, then this is how I would do it:
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float speed = 10f;
float x = Input.GetAxis(“Horizontal”) * speed * Time.deltaTime;
float z = Input.GetAxis(“Vertical”) * speed * Time.deltaTime;
rb.AddForce(x, 0f, z);
}
And freeze rotation along the x and a axis to not fall over. You can do this in the rigidbody component.

Unity Script error CS1061 'Vector2' does not contain a definition for 'GetAxis'

The full error message I am receiving in Unity and I am working on a mac.
Assets/Scripts/Controller.cs(19,45): error CS1061: 'Vector2' does not contain a definition for 'GetAxis' and no accessible extension method 'GetAxis' accepting a first argument of type 'Vector2' could be found (are you missing a using directive or an assembly reference?)
Here is the code I am using:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public float speed;
private Vector2 moveVelocity;
private Rigidbody2D rigidBody;
void Start() {
rigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update() {
Vector2 moveInput = new Vector2(moveInput.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
moveVelocity = moveInput.normalized * speed;
}
void fixedUpdate () {
rigidBody.MovePosition(rigidBody.position + moveVelocity * Time.fixedDeltaTime);
}
}
Please let me know if there is any additional information needed.
I think you got Input and moveInput confused. Also, if you have a RigidBody or RigidBody2d you should try and not modify the position of GameObjects directly, instead using the AddForce method. I would remove the fixed update method and instead modify your Update method to be the following:
void Update() {
Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"));
rigidBody.AddForce(moveInput.normalized * speed);
}

How do I fix this parameter error when running game using unity game engine?

Hi everyone this is my first Q.
I get this error on the console tab when I run the game.
Parameter 'speed' does not exist.
UnityEngine.Animator:SetFloat(String, Single)
PlayerMovement:FixedUpdate() (at Assets/Scripts/PlayerMovement.cs:30)
Could anyone please help me find the root of the problem please?
Thanks =)
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private Animator playerAnimator;
private float moveHorizontal;
private float moveVertical;
private Vector3 movement;
// Use this for initialization
void Start () {
playerAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
moveHorizontal = Input.GetAxisRaw ("Horizontal");
moveVertical = Input.GetAxisRaw ("Vertical");
movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
}
void FixedUpdate () {
if (movement != Vector3.zero) {
playerAnimator.SetFloat ("speed", 3f);
} else {
playerAnimator.SetFloat ("speed", 0f);
}
}
}
Noob mistake! "speed" should be capitalized. I should have "Speed", but have typed "speed".
Just incase anyone else has this problem...

C# error : An object reference is required for the non-static field, method, or property line 12

using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour {
public float maxSpeed = 6.0f;
public bool FacingRight = true;
public float moveDirection;
// Use this for initialization
void FixedUpdate () {
Rigidbody.velocity=newVector2(moveDirection*maxSpeed,Rigidbody.velocity.y);
/**the error is in this line this is a simple game where the player just move left and right
where when the D +ve number is clicked the player moves forward (right) **/
}
// Update is called once per frame
void Update () {
moveDirection = Input.GetAxis("Horizontal");
}
}
You need to take your RigidBody Component from your game object in order to use it. Change your fixedUpdate function with this :
void FixedUpdate ()
{
RigidBody rigidbody = gameObject.GetComponent<RigidBody>();
rigidbody.velocity = new Vector2(moveDirection*maxSpeed,rigidbody.velocity.y);
}