Quantcast
Channel: Answers by "ThePersister"
Viewing all articles
Browse latest Browse all 99

Answer by ThePersister

$
0
0
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); } /// /// Check is grid cube free to use / does it overlap with specific object /// /// a boolean to state whether the GameObject *isInTrigger* was or was not within this trigger. 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); } /// /// Check is grid cube free to use / does it overlap with specific object /// /// Object to check with. /// a boolean to state whether the GameObject *isInTrigger* was or was not within this trigger. 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 :) 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 :) Yours truly, ThePersister

Viewing all articles
Browse latest Browse all 99

Trending Articles