NullReferenceException: Object reference not set to an instance of an object
WinableCondition.OnTriggerEnter (UnityEngine.Collider player) (at Assets/ThisGameFolder/WinableCondition.cs:40)
i already put all the object reference to an instance of an object but warning still appears… anyone can help???
This is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WinableCondition : MonoBehaviour {
public Text text;
private static bool readyToWin;
float flySpeed = 5f;
public GameObject playerMovScript;
private void Start()
{
text.gameObject.SetActive(false);
readyToWin = false;
}
private void Update()
{
if(CollectGasCan.count >= 8)
{
readyToWin = true;
}
if(Input.GetKeyDown(KeyCode.C))
{
CollectGasCan.count = 8;
}
}
private void OnTriggerEnter(Collider player)
{
if(player.CompareTag("Player") && !readyToWin)
{
text.gameObject.SetActive(true);
}
else if (player.CompareTag("Player") && readyToWin)
{
playerMovScript.GetComponent<PlayerMovement>().enabled = false;
text.gameObject.SetActive(true);
text.text = "You Won!";
Flying();
}
}
private void OnTriggerExit(Collider player)
{
if (player.CompareTag("Player"))
{
text.gameObject.SetActive(false);
}
}
void Flying()
{
transform.Translate(0f, flySpeed * Time.deltaTime, 0f);
}
}
Does your Player object have a PlayerMovement component attached to it? That’s most likely what’s causing the error here.
Also, kudos for posting the entire script, using code tags, the full error, and a screenshot of your editor! That’s the way to get help around here!
there is a PlayerMovement on Player object… still have no idea what causes the error appear…
I am trying to disable PlayerMovement on Player object when entering the capsule trigger… is there an another way to disable script from another script besides the above code?
I’ve got a sick 2 year old that, apparently, needs my constant attention right now or she’s going to trash the entire house. So I’m a bit too distracted to try and suss this out myself.
Try throwing a bunch of Debug.Log() messages around your script, especially in that OnTriggerEnter method. See if it’s getting called on objects that it shouldn’t be getting called on, for example. It may be that something else is colliding with this object that has a Player tag?
1 Like
@Schneider21 's suggestion is a good one.
Also, you don’t need to declare your playerMovScript property as type GameObject. Go ahead and declare it as type PlayerMovement. Then drag it in again, since it will have lost its reference to the player object. Now instead of having a reference to the player GameObject, on which you need to call GetComponent to get the PlayerMovement, you have a reference directly to the PlayerMovement itself — no need for GetComponent at all.
1 Like
@JoeStrout i have tried it but as you can can see in the video below i can’t put my player or PlayerMovement into the box…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WinableCondition : MonoBehaviour {
public Text text;
private static bool readyToWin;
float flySpeed = 5f;
public PlayerMovement playerMovScript;
private void Start()
{
text.gameObject.SetActive(false);
readyToWin = false;
}
private void Update()
{
if(CollectGasCan.count >= 8)
{
readyToWin = true;
}
if(Input.GetKeyDown(KeyCode.C))
{
CollectGasCan.count = 8;
}
}
private void OnTriggerEnter(Collider player)
{
if(player.CompareTag("Player") && !readyToWin)
{
text.gameObject.SetActive(true);
}
else if (player.CompareTag("Player") && readyToWin)
{
playerMovScript.enabled = false;
text.gameObject.SetActive(true);
text.text = "You Won!";
Flying();
}
}
private void OnTriggerExit(Collider player)
{
if (player.CompareTag("Player"))
{
text.gameObject.SetActive(false);
}
}
void Flying()
{
transform.Translate(0f, flySpeed * Time.deltaTime, 0f);
}
}
https://www.youtube.com/watch?v=jFrZ4ash2Cg
Not sure what is wrong there. That should work 
Wait a second, how do you have two Inspector panels? That’s blowing my mind! I thought when I saw it before that it was some kind of composite screenshot… I just… didn’t know this was possible!
Right click on the tab → Add tab → (whatever) 
Welp, learn something new every day…
But how does the Inspector 2 know which object to display? This warrants further investigation.
I tried a quick test and couldn’t get them to show a new selection based on “their focus”; so I had to lock one then choose something else and that updated the unlock one(s). 
Would be nice if it worked with focus… =)
I think you just solved @Disastah 's latest problem, then, @methos5k ! The Inspector on the right side does appear to be locked. So dragging the object with the PlayerMovement script on it won’t perform the assignment.
Lesson: Showing multiple Inspectors is not worth the trouble.