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 /
  • Help Room /
This question was closed Oct 23, 2016 at 12:16 AM by Zitoox for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by Zitoox · Oct 22, 2016 at 03:42 PM · javascriptbug-perhapsontrigger

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.

Comment
Add comment · Show 4
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 Zitoox · Oct 22, 2016 at 03:43 PM 0
Share

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.

avatar image tanoshimi · Oct 22, 2016 at 04:43 PM 1
Share

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.

avatar image Zitoox tanoshimi · Oct 22, 2016 at 08:24 PM 0
Share

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.

avatar image Zitoox tanoshimi · Oct 23, 2016 at 12:16 AM -1
Share

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.

2 Replies

  • Sort: 
avatar image
1
Best Answer

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.

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
0

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.

Comment
Add comment · Show 1 · 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 Zitoox · Oct 22, 2016 at 08:41 PM -1
Share

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...

Follow this Question

Answers Answers and Comments

88 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

Related Questions

Enoent error: diskusage.node not found when starting Unity Hub 0 Answers

Object drifting to left or right without reason 1 Answer

Gameobject reference disappears at start 1 Answer

Why can't I move player's game piece on 2nd turn? 0 Answers

Walking animation javascript? 2 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