- Home /
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;
}
}
}
Answer by prakashxavier · Jun 01, 2020 at 08:36 AM
Got it to work after I downloaded the latest version of Unity!
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?
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.
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.
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.
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:
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
Follow this Question
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