Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Avash · Nov 27, 2014 at 06:00 PM · c#collider

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Nov 27, 2014 at 06:03 PM 0
Share

If the isTriggered call happens after the OnTriggerEnter call, the only reason I can think of for it being null when you get to isTriggered is that the collider (or it's gameobject) has been destroyed in the meantime.

avatar image Avash · Nov 27, 2014 at 06:47 PM 0
Share

Yes it will be destroyed, but I think it should happen after running functions.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ThePersister · Nov 28, 2014 at 09:20 AM

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 :) 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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to transfer colllider from OnTriggerEnter to Update 1 Answer

Two exact objects behaving differently... *scratches head* 0 Answers

Animated Texture Offset 1 Answer

A problem with intersection detection 1 Answer

Ignore/Destroy then re-enable Mesh Colliders 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges