Object reference not set to an instance of an object ERROR - Idk why
Whats the correct way it should be written?
using UnityEngine;
using System.Collections;
public class GroundCheck : MonoBehaviour {
private Player player;
void Start () {
player.gameObject.GetComponentsInParent<Player> ();
}
void OnTriggerEnter2D(Collider2D col){
if (null == player) {
Start ();
}
player.grounded = true;
}
void OnTriggerStay2D (Collider2D col){
player.grounded = true;
}
void OnTriggerExit2D(Collider2D col){
player.grounded = false;
}
}
Answer by Dibbie · Jun 16, 2016 at 03:52 PM
Im noticing a few weird things...
On line 6, you have private Player player
, then line 10, you have player.gameObject.GetComponentsInParent<Player>();
, but why? You are linking Player (which I assume is NOT another script), and trying to get the component of something that (most likely) doesnt exist on... Itself? What are you trying to look for with line 10 exactly? What component specifically? Unless "Player" is a script, there is no Unity component named "Player".
Line 16, I think your if statement is backwards, it should probably be if(col.transform = player)
assuming your "player" is a Transform
You should never call "Start", the purpose of it is to just run only once, ever in your script, being the "entry function" or the very first function thats fired up, executed, and then never messed with again - create another function to do that and call that instead, and since its one line of code, its probably better to just use Line 10 in replacement of Line 17, unless you plan on making it much longer later?
I think the biggest issue is how you created "player" as a reference to "Player", that it just doesnt know what your talking about. What exactly is "Player" and where does it exist?
@Dibbie can you look at my question I think you would be able to help. I posted it about 40 $$anonymous$$utes ago.
Here is a link. It is also in the null reference exception section. Thanks!
http://answers.unity3d.com/questions/1203688/null-reference-exception-with-custom-class-object.html
Your answer
Follow this Question
Related Questions
Null Reference Exception Error 1 Answer
Can someone make sense out of seemingly inconsistent null reference exception? 3 Answers
is my enemy selector null reference a problem? 1 Answer
GameObject only spawning 60% of the time (c#) 1 Answer
ArgumentNullException: Value cannot be null. parameter name: Source 1 Answer