can someone please help me, im stuck now for 2 days and I can’t find what’s wrong
this is my code.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
public class Player : MonoBehaviour
{
[SerializeField] private float moveSpeed = 7.5f;
[SerializeField] private bool Start_working = false;
private Rigidbody2D rb;
private float moveInput;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rb = GetComponent<Rigidbody2D>();
Move();
}
private void Move()
{
moveInput = UserInput.instance.moveInput.x;
rb.velocity = new Vector2(moveInput * 7.5f, rb.velocity.y);
}
}
I’m new so idk if there are already resource to solve this problem, but I couldn’t find it.
Lots of someone’s can help but I’ll point out that you (seem) to be missing a fundamental understanding of what components are. Not in a bad, bad way but in a “you need to understand this” sort of way.
Ask yourself “what is a Rigidbody” and why do I need a reference to it? Why would I need to get this rb component “once per frame”? Could it possibly be different from frame to frame?
You might question all those using statements (at the top) as well. Are they there for a reason or just obscuring what is needed for no apparent benefit?
In any case, for now, define the Rigidbody as a serialized field (like your other ones) and assign it in the inspector.
As tley points out, the above has a lot of random misuse issues.
You may wish to start with another tutorial, any one of the Unity ones.
Make sure you don’t just do them without understanding them, or you’re wasting your time.
Two steps to tutorials and / or example code:
do them perfectly, to the letter (zero typos, including punctuation and capitalization)
stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
Thank you, about configuring the Rigidbody, I tested if the Start method worked, but I didn’t chance it back after testing. But I wil try to define it as an serialized field. brb.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
public class Player : MonoBehaviour
{
[SerializeField] private float moveSpeed = 7.5f;
[SerializeField] private bool Start_working = false;
[SerializeField] private Rigidbody2D rb;
private float moveInput;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
moveInput = UserInput.instance.moveInput.x;
rb.velocity = new Vector2(moveInput * 7.5f, rb.velocity.y);
}
}
@Kurt-Dekker I’m trying to do the first step of identify what is null, but cant find it, this is the error, maybe you know what’s wrong. thx tho ( :
Before you move forward with your “game” let me suggest that you move forward with an exploration of what you already have at your disposal and what you gain by adding Debug statements to your code.
Your tools showed the error and a trace of how it got to the error and the exact line where the error occurred. If you get in the habit of adding Debug statements you can determine if references are null, and if values are what you believe them to be.
Don’t drive the car until you know where the brake pedal is and how to use it.