Grid snapping script problem

BuildScript.cs - Pastebin.com GridController.cs - Pastebin.com gridTrigger.cs - Pastebin.com GridTrigger.cs

In gridTrigger.isTriggered variable collider is always null. Why?

Looks like your problem is somewhere in this vicinity:

 foreach (GameObject gridCube in gridCubesList) {
        if(gridCube != null) {
             gridTrigger trigger = gridCube.GetComponent<gridTrigger>();
             if(trigger.isTriggered(GroundObject)) {
                 localPositionsList.Add(this.transform.position - gridCube.transform.position);

                 // EHTO EI TÄYTY etsi ongelma
                 GroundObjectCubesList.Add(gridCube);
             }
             if(trigger.isTriggered(null) != true){
                  if(gridCube != null){
                        freeCubesList.Add(gridCube);
                  }
             }
        }
}

You’re nicely catching whether gridCube is null or not, good job on that. However, gridCube may still not contain a gridTrigger object, thus filling trigger with Null, it’s likely that your error comes from that occassion.

The reason why I’m pointing out this NullReference specifically is because Booleans can never be null values, so it just Has to be trigger being null in certain cases.

I suggest looking into that, looking at the code, you should have the experience to get through it :wink:

But if you have any questions, feel free to ask, also making a screenshot of the error itself and where it leads to in the script may help if the problem doesn’t lie near the trigger.

If this answer was satisfactory, please accept it, and either way, have a nice day! :slight_smile:

Yours truly,
~ThePersister

**-

-**
EDIT:

Oh, my apologies, looks like I misread the question.
The actual answer is much more easy than this XD

Your issue is the collider in the gridTrigger script always being null, correct?
Well
“collider” is an already existing variable that every monobehaviour holds, it’ll look for the collider on the gridTrigger.gameobject instead of the variable instead, because of some name conflict.

Try renaming the variable Collider collider to something like Collider lastCollider.
That should solve the problem.

If it doesn’t, you should consider looking into whether the OnTriggerEnter method is properly called or called at all. :slight_smile:

Good luck! :smiley: