Can't transfer colllider from OnTriggerEnter to a public bool

using UnityEngine;
using System.Collections;

public class gridTrigger : MonoBehaviour {

	Collider lastCollider;

	// Use this for initialization
	void Start () {
	}
	
	void OnTriggerEnter(Collider other) {
		lastCollider = other;
		Debug.Log (lastCollider);
	}

	// Update is called once per frame
	void Update () {
	}
	// Check is grid cube free to use / does it overlap with specific object
	public bool isTriggered (GameObject isInTrigger){
		Debug.Log(lastCollider);
		bool localBool = false;
		if(lastCollider != null){
	//		Debug.Log ("toimii");
			if(isInTrigger != null){
				if(lastCollider == isInTrigger.collider){
					localBool = true;
				}	
			}
			if(isInTrigger == null){
				localBool = true;
			}
		}
		if (lastCollider == null){
			localBool = false;
		}
		lastCollider = null;
		return localBool;
	}
}

Line 14 returns a collider and line 22 null. Why? isTriggered is called from another script.

Here’s a simplified version of your code, a bit neater as well, with some commments.

using UnityEngine;
using System.Collections;

public class gridTrigger : MonoBehaviour
{
    Collider lastCollider;

    void OnTriggerEnter(Collider other)
    {
        lastCollider = other;
        Debug.Log(lastCollider);
    }

    /// <summary>
    /// Check is grid cube free to use / does it overlap with specific object
    /// </summary>
    /// <returns>a boolean to state whether the GameObject *isInTrigger* was or was not within this trigger.</returns>
    public bool isTriggered()
    {
        // Optional, debugging just lastCollider will result in errors when it is actually null, so don't. :3
        Debug.Log(lastCollider == null); 

        // lastCollider being null is your only false case, so everything else results in true, correct? 
        // well, then this is the simplified variant.
        bool localBool;
        if (lastCollider == null) localBool = false;
        else localBool = true;

        // Note, we're not immediately returning just so we can set the lastCollider to null, 
        // not sure why, maybe you don't want to check a collider twice, but regardless, we're doing it.
        lastCollider = null;
        return localBool;
    }
}

Having simplified your code, I noticed that only when “lastCollider” isn’t null, you return false.
Assuming your method “isTriggered” checks to see if the last object in the collider is equal to the one sent in the parameter, due to your coding, you’re basically saying. Oh so you’re an object, yepp, that was it.

I could even take away the parameter and have it still work.

Right now, ANY object you send in the isTriggered method will return true, as long as lastCollider is not null, which well, doesn’t make sense x)

Instead, maybe you should use:

using UnityEngine;
using System.Collections;

public class gridTrigger : MonoBehaviour
{
    GameObject lastTriggeredObject;

    void OnTriggerEnter(Collider other)
    {
        lastTriggeredObject = other.gameObject;
        Debug.Log(lastTriggeredObject.name);
    }

    /// <summary>
    /// Check is grid cube free to use / does it overlap with specific object
    /// </summary>
    /// <param name="possiblyLastTriggeredObject">Object to check with.</param>
    /// <returns>a boolean to state whether the GameObject *isInTrigger* was or was not within this trigger.</returns>
    public bool isLastTriggered(GameObject possiblyLastTriggeredObject)
    {
        // Declare localBool
        bool localBool = false;

        // Prevent null catches, then compare gameObjects, if those are equal return true, otherwise return false.
        if (lastTriggeredObject == null || possiblyLastTriggeredObject == null) localBool = false;
        else if (lastTriggeredObject == possiblyLastTriggeredObject) localBool = true;

        // Note, we're not immediately returning just so we can set the lastCollider to null, 
        // not sure why, maybe you don't want to check a collider twice, it's suspicious....
        lastTriggeredObject = null;
        return localBool;
    }
}

I bet you can take it from here :slight_smile:
If you have any more questions or want to share more details, let me know, I’ll give it another shot, otherwise, if this answer helps you enough, please accept it and continue your programming journeys :slight_smile:

Yours truly,
ThePersister