Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 prakashxavier · May 29, 2020 at 05:13 AM · guieditorbuild

OnGUI() works in editor, but not in build with an odd twist!

I have a ship moving, that checks for collision as well as trigger. When ship collides with another ship it gives a collision status message.

I have another object that has checks for trigger.

In Editor My onGUI() function works perfectly for both collision as well as trigger

In Build OnCollisionEnter() and OnCollisionExit() works against another ship which detects collision and not trigger.

OnTriggerEnter() and OnTriggerExit() DOES NOT WORK which means

GUI.Box(new Rect(winX, winY, 200, 100), content); doesn't show up with my status messages.

 public class OnDriftingCourse : MonoBehaviour
 {
     public Boolean collided = false;
     GUIContent content;
     public Texture BoxTexture;
     public int winW = 200;
     public int winH = 100;
     private int winX;
     private int winY;
 
    
 
 
     void OnTriggerExit(Collider c)
     {
         collided = true;
         content = new GUIContent(" Trigger Exited " , BoxTexture, "--");
         print("ON Trigger Exit");
     }
 
     void OnCollisionEnter(Collision collision)
     {
         collided = true;
         content = new GUIContent(" Collided enter ", BoxTexture, "--");
         print("ON Colission Enter");
     }
 
     void OnCollisionExit(Collision collision)
     {
         collided = false;
         //content = new GUIContent(" Collided enter ", BoxTexture, "--");
         //print("ON Colission Enter");
     }
 
 
     void OnTriggerEnter(Collider c)
     {
         collided = true;
         content = new GUIContent(" Trigger ENTER ", BoxTexture, "--");
         print("ON Trigger Enter");
 
     }
 
 
     void OnGUI()
     {
         winX = (Screen.currentResolution.width) / 2;
         winY = (Screen.currentResolution.height) / 2;
 
         winX = 600; winY = 50;
         if (collided)
         {
             GUI.Box(new Rect(winX, winY, 200, 100), content);
         }
         else
         {
             collided = false;
         }
             
 
     }
 }
 
 


Comment
Add comment
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

2 Replies

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

Answer by prakashxavier · Jun 01, 2020 at 08:36 AM

Got it to work after I downloaded the latest version of Unity!

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
avatar image
1

Answer by SteenPetersen · May 29, 2020 at 05:23 AM

Hi @prakashxavier

Could it be that perhaps you are setting collided to true when it should be false in OnTriggerExit?

      void OnTriggerExit(Collider c)
      {
          collided = true; // should this be false instead?
          content = new GUIContent(" Trigger Exited " , BoxTexture, "--");
          print("ON Trigger Exit");
      }


Also do these colliders overlap each other in a weird way somehow?

Comment
Add comment · Show 6 · 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
avatar image prakashxavier · May 29, 2020 at 06:47 AM 0
Share

Thanks for your reply Steen

collided = true is only a flag that I use to trigger my GUI box.

the colliders dont overlap i've seperated them.

The issue really seems to be that it works differently after build and in editor. Works just fine in editor.

avatar image SteenPetersen prakashxavier · May 29, 2020 at 06:50 AM 1
Share

Hey converted your "answer" to a "comment", remember to distinguish between the two. Why do you need both a trigger and a collision check it seems a bit odd, perhaps I could help better if I understood what exactly you were trying to achieve.

avatar image prakashxavier · May 29, 2020 at 06:58 AM 0
Share

Sorry this is my very first post in the forum here.

The reason to have both trigger and collision is.

Its a typical port scenario where I have ships. When my ship collides against another ship i need the collision to have physics. Trigger is used so that when my ship deviates from a specific route it should trigger a warning. I've created a plane that depicts the route and when the ship is out of the route(plane) i need it send a warning. Hope this clarifies. Thanks again.

avatar image SteenPetersen prakashxavier · May 29, 2020 at 07:14 AM 0
Share

Ok, so that sounds logical, but I would suggest for something like this that you use raycasting ins$$anonymous$$d as it is a great deal more performant. You can almost imagine a collider as a raycast in all possible directions when you actually only need raycasts in a few specific directions. So for eg you could shoot a raycast from both sides of your ship and check on a layermask that you call 'Route' to see if it is still staying on course. This script should be able to get you started.

 [SerializeField] private Layer$$anonymous$$ask route;
 RaycastHit rightRay, leftRay;
 
 private void Update()
 {
     if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out rightRay, $$anonymous$$athf.Infinity, route))
     {
         Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * rightRay.distance, Color.yellow);
         Debug.Log("Hit Right");
     }
 
     // note the "-Vector3.right" because there is no such thing as left 
     if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.right), out leftRay, $$anonymous$$athf.Infinity, route))
     {
         Debug.DrawRay(transform.position, transform.TransformDirection(-Vector3.right) * leftRay.distance, Color.green);
         Debug.Log("Hit Left");
     }
 }


And here you can see the result:

alt text

avatar image prakashxavier · May 29, 2020 at 07:30 AM 0
Share

Thanks so much for this. Let me go check it out.

avatar image SteenPetersen prakashxavier · May 29, 2020 at 07:47 AM 0
Share

Anytime, if it helps let me know, and ill update my answers. remember if it helps or solves your issue mark it as correct and upvote it, to make it easier for other to find the answers in the future as well.

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

263 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Script work in editor but not in build 0 Answers

GUI works flawlessly in editor, but will not show in standalone build. 1 Answer

GUI Score Overlapping 1 Answer

uGUI objects not Instantiate()ing in executable 0 Answers

Distribute terrain in zones 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