The question is answered, right answer was accepted
Rigid body can't find the instance
Hi i'm getting baffled by a "NullReferenceException: Object reference not set to an instance of an object". in my code for the player i have
//for handling of player
protected Rigidbody rb;
protected Collider col;
// Start is called before the first frame update
void Start()
{
//getting live object data
rb = GetComponent<Rigidbody>();
col = GetComponent<Collider>();
if (rb = null)
{
Debug.Log("player's rigidbody is null");
}
if (col = null)
{
Debug.Log("player's Collider is null");
}
//rigidbody test for example
rb.velocity = new Vector3(0,0,1);
}
i get the Null referance on :
rb.velocity = new Vector3(0,0,1);
Double check everything i can think of. Script is attached to game object and the rigidbody is on that same game object. the null checks are not coming up in the Debug log.
So I've hit a block and have come for some help
Answer by Vega4Life · Jul 20, 2019 at 06:05 PM
if (rb = null)...
You are assigning it null in the if statement.
It needs to be If (rb == null). :)
Answer by Kavery88 · Jul 20, 2019 at 06:05 PM
My mistake as soon as i postyed it i got it figured. my conditional statements like:
if (rb = null)
are using assignment operators in tead of '=='
been spending to much time in excell and vba
Follow this Question
Related Questions
error CS0103 rb doesn't exist in the current context, ligne 8 and 15 , can't fix it 1 Answer
Why is new Vector3 stronger when player is in the air? 0 Answers
Object and first-person controller's collision not being registered 1 Answer
how can I add Sprint into my Script ?,How can I add Sprint function ?? 0 Answers
How to move forward (towards z), ignoring any rotations? 0 Answers