Error when I try to assign a variable to Rigidbody2D - c#

When I try adding Rigidbody2D to the script it keeps giving me the error CS0428: "Cannot convert method group 'GetComponent' to non-delegate type 'Rigidbody2D'. Did you intend to invoke the method?". MAybe its something im not seeing but ive went through it several times and didnt see anything wrong.
using UnityEngine;
public class characterController : MonoBehaviour
{
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}

I think you need to add brackets after Rigidbody2D like this:
using UnityEngine;
public class characterController : MonoBehaviour {
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}
To avoid this problem again, try reading the error message carefully. For example, this problem could have been easily avoided by reading docs on GetComponents.

You missed the parentheses after GetComponent
using UnityEngine;
public class characterController : MonoBehaviour
{
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}
Instead of running the GetComponent() method and setting rb as what it returns, the code tries setting it as a delegate.

Related

Unity says "error CS1022: Type or namespace definition, or end-of-file expected"

I am making a cross board game using Unity based on a tutorial. This problem occurs when I add a jump animation.
All my code is from that tutorial and no changes have been made except for one value.
I know which line of code is wrong but I don't know why and I don't know how to fix it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playermovement : MonoBehaviour
{
private Rigidbody2D rb;
public float jumpAmount = 1;
bool isGrounded;
public Transform groundCheck;
public LayerMask groundlayer;
public Animator anim;
public float overlap = 0.5f;
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (isGrounded)
{
Jump();
}
}
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, overlap, groundlayer);
}
void Jump()
{
rb.AddForce(Vector2.up *5* jumpAmount, ForceMode2D.Impulse);
}
if(isGrounded)
{
anim.SetBool("Jump",false);
}
else
{
anim.SetBool("Jump", true);
}
}
Does anyone know what's wrong with this, please?
Thank you in advance to those who answered!
Assets\Script\Playermovement.cs(52,1): error CS1022: Type or namespace definition, or end-of-file expected
This is the error message I received. I know it tells me the error occurred on the first character of line 52 (which is the last line), but other than that I don't know anything because there is only one curly bracket on that line.
The problem is with if (isGrounded) statement. The if/else statement needs to be in a method/function, and can't work without being in one.

Unity accessing rigidBody component with input system

I have just started a new Unity tut using its input system to move a ball. However, the script doesn't seem to be working when I try to move the ball
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem; //Namespace for accessing InputSystem to control ball
public class PlayerController : MonoBehaviour
{
private Rigidbody rb;
private float movementX;
private float movementY;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Onmove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement);
}
}
If you did everything else right and the problem is only in your script then changing from Onmove to OnMove should fix problem
Create in Unity a new InputControl with the name PlayerInputActions and into
Inspector select Generate C# class, Unity autogenerate for you Action Maps , Actions and Binding Property, double click on it to watch, than in a new c# script named NewInputSystem
using UnityEngine;
using UnityEngine.InputSystem;
public class NewInputSystem : MonoBehaviour
{
public Rigidbody rb;
public float moveSpeed = 7.0f;
Vector2 moveDirections = Vector2.zero;
public PlayerInputActions playerControls;
private InputAction move;
private void Awake()
{
playerControls = new PlayerInputActions();
}
private void OnEnable()
{
move = playerControls.Player.Move;
move.Enable();
}
private void OnDisable()
{
move.Disable();
}
void Start()
{
rb =gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
moveDirections = move.ReadValue<Vector2>();
}
private void FixedUpdate()
{
//change how you want
rb.velocity = new Vector3(moveDirections.y * moveSpeed, 0, moveDirections.x * -moveSpeed);
}
}

Unity can't find OnMove function of New Input System

I have been working on unity's new ınput system and I have issue.I want use OnMove function of Player Input component but I'm getting this error:
MissingMethodException: PlayerMovement.OnMove Due to: Attempted to access a missing member.So my character don't move.How can I fix this ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
Vector2 moveInput;
Rigidbody2D myRigidbody;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
Run();
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed,
myRigidbody.velocity.y);
myRigidbody.velocity = playerVelocity;
}
}
What is a TileVania?
I would expect a InputAction.CallbackContext there I guess.
Sounds to me like the PlayerInput is trying to call your method via SendMessage but the signature is not correct and it should rather be
void OnMove(InputAction.CallbackContext context)
{
moveInput = context.Get<Vector2>();
}
You can fix this by adding the
[RequireComponent(typeof(Rigidbody2D))]
attribute to your PlayerMovement script. This will make sure that your script always has a Rigidbody2D component when it's added to a game object.

The name 'rb' does not exist in the current context?

I am pretty new to c# and unity, I decided to make a test game were you can control the player, the thing is I keep getting this error: 'The name 'rb' does not exist in the current context'. I have no clue what this means, but this is what I have done.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement_controller : MonoBehaviour
{
public float Speed = 1.0f;
void Awake()
{
rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D;
rb.bodyType = RigidbodyType2D.Kinematic;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
rb.velocity = (Speed, 0.0f);
}
}
}
You have to assign rb as a variable.
public float Speed = 1.0f;
private Rigidbody2D rb;
void Awake()
{
rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D;
rb.bodyType = RigidbodyType2D.Kinematic;
}
you have only called rb within the awake function, but you have not assigned it as a global variable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement_controller : MonoBehaviour
{
public float Speed = 1.0f;
public RigidBody rb; //<====
void Awake()
{
rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D;
rb.bodyType = RigidbodyType2D.Kinematic;
}

Unity Prefab The name `mainCamera' does not exist in the current context

I am getting the error The name mainCamera' does not exist in the current context for the linetargetPos = (Vector2)mainCamera.main.ScreenToWorldPoint(Input.mousePosition);`. I have search for an answer but cannot find a way to stop this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
float speed = 2f;
Vector2 targetPos;
private Rigidbody2D myRigidbody;
private Animator myAnim;
private static bool playerExists;
public GameObject cameraPrefab;
private void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
if(!playerExists){
playerExists = true;
DontDestroyOnLoad(transform.gameObject);
} else {
Destroy(gameObject);
}
targetPos = transform.position;
GameObject mainCamera = (GameObject)Instantiate(cameraPrefab);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
targetPos = (Vector2)mainCamera.main.ScreenToWorldPoint(Input.mousePosition);
}
if ((Vector2)transform.position != targetPos)
{
Move();
} else {
myAnim.SetBool("PlayerMoving", false);
}
}
You're getting that particular error because mainCamera is a local variable defined in Start. It is out of scope where you try to reference it in Update. You probably meant to define it as a field in your class, so you could reference it with mainCamera anywhere in your class. To do that you should do this instead:
// ...
private Rigidbody2D myRigidbody;
private Animator myAnim;
private static bool playerExists;
public GameObject cameraPrefab;
public GameObject mainCamera; // add this line
private void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
if(!playerExists){
playerExists = true;
DontDestroyOnLoad(transform.gameObject);
} else {
Destroy(gameObject);
}
targetPos = transform.position;
mainCamera = (GameObject)Instantiate(cameraPrefab); // use mainCamera field
mainCamera.tag = "MainCamera"; // tell Unity that it is your main camera.
}
// ...
But anyway, Camera.main is a static property of the Camera class, so you should access it through the Camera class anyway.
You should use this in Update instead:
targetPos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);

Categories