The question is answered, right answer was accepted
OnTrigger function problem
EVERYTIME i use OnCollision or anything that uses triggers, my game glitches. Here are two examples:
1- I have a cube far away from me, and i am using the OnTrigger function so when the player hits it, the screen goes black. When i start the game, i just need to walk a few meters and my screen will be black even if i am still far away from the object.
2- I have a sphere far away from me, and i want to show a log when i hit it. I start the scene, walk a little and bam! The log is displayed and i am far away from the sphere.
Here is an example that i recently made:
#pragma strict
var Img : UI.Image;
private var Secure = false;
function Start()
{
Secure = false;
Img.enabled = false;
}
function OnTriggerEnter (other:Collider)
{
Secure = true;
Img.enabled = true;
}
function OnTriggerExit (other:Collider)
{
Secure = false;
Img.enabled = false;
}
I made the "Secure" variable just to test, and it didn't solved anything. I think i am missing something here. Could anyone help me? I think i am doing something wrong. If everything is fine, please test it in one of your games to see if the same happens.
It ONLY works in one scene of my project, strangely. All the other scenes have the same bug. I didn't tried in another project, but i think that the same will happen.
OnTriggerEnter, OnCollisionEnter etc. are so fundamental to most games that if there was a bug in them, it would be noticed immediately by millions of players. The fact that this hasn't happened means we can safely conclude it's a mistake in your implementation.
Note that they don't care how far away or what size your meshes are - only in the interaction between their colliders. Look at scene view when your game is running and take note of the green outlines - when they touch, that's what fires OnCollisionEnter.
Yeah, this is kinda obvious.I know that i am a beginner, but i am not that noob. (At least i think xD)
And as far as i know i didn't say that it was a global bug, just for me.
I discovered what was causing it. BUT there is another scene in wich is literally a bug. Unfortunately i will need to "update" my engine... This is literally the worst thing about Unity... anyway, question solved.
Answer by JincSoft · Oct 22, 2016 at 04:40 PM
From the code given, you can remove the Secure variable because it does nothing. One thing I would check is if there is any game object withing the bounds of the collider on the object you have this script on. The way it is now it will register ALL gameobjects that come into contact with it as opposed to the player object which you can fix with dropping:
if(other.gameobject.tag == "Player")
{
//code goes here
}
or
if(other.gameobject.name == "PlayerObjectNameHere")
{
//code goes here
}
inside the OnTriggerEnter/OnTriggerExit functions.
Personally I would use the tag because there is far less of a chance of it changing over the course of development, just don't forget to set the tag of the player object to Player.
Answer by ananasblau · Oct 22, 2016 at 04:52 PM
Most likely you hit something else and as you don't do a check for what you hit in your OnTrigger it goes BAM! Put a Debug.Log(other.gameObject.name);
into your OnTrigger and see what you've actually hit.
I am hitting in another cube that i called "RADAR". It has the is trigger checkbox activated too, but i didn't get what is happening!
Radar doesn't have any kind of script, and as far as i know, my script was supposed to work ONLY in the object that i have inserted the script. It wouldn't do any sense if it worked like: Just because it has ontrigger it works with anything that has a trigger because i already have other objects with trigger on them and it doesn't change ANYTHING. This is VERY STRANGE!
Edit:
I got it! @JincSoft in his answer it is written "if there is any game object withing the bounds of the collider" BUT it wasn't suppose to do anything, as it's collider doesn't have this option. I think my question is solved, but i don't know who to accept...