• 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 Jenkins6161 · Aug 14, 2013 at 03:03 AM · ontriggerexit

How to fix variables rapidly changing true/false

First off my game was running fine in the unity developer. I made a web player build and some things went wonky in the build(player death wasn't working properly and forces were different then in dev preview). The force thing was easy enough to fix by changing the number slightly but the death thing has me and my buddy stumped. Heres my code:

Basicly after the earthquake hits(boolean EQ) it checks to see if your in a safe area. If your not it starts the death timer and after 7 seconds of not being safe you die.

 var player: GameObject;
 var EQ : boolean;//earthquake
 var danger : boolean;
 var training : boolean;
 var alive : boolean;
 var safe : boolean;
 var deathTimer : float;
 var waitTime : int = 7;
 
 function Start() {
 player = gameObject;
 EQ = false;
 danger = false;
 training = true;
 alive = true;
 safe = false;
 deathTimer = 0;
 waitTime = 7;
 }
 
 function Update() {
     if (training) {
         danger = false;
     } else if (EQ&&safe) {
         danger=false;
     } else if (EQ&&!safe) {
         Debug.Log("DANGER");
         danger = true;
     }
     if (safe) {
         deathTimer=0;
     }
 }
 
 function FixedUpdate() {
     //Death timer
     if (danger) {
         deathTimer += Time.deltaTime;
         if (deathTimer > waitTime) {
             alive = false;
             deathTimer = 0;
         }
     }
 }

The problem is it doesn't reset the timer. The timer continues to run because for some reason the var 'safe' flips back and forth between true and false rapidly. This is my trigger code that is in the safe area:

 function OnTriggerEnter (myTrigger : Collider) {
     if ((myTrigger.gameObject.name == "FPSController")) {
         player.GetComponent(Death).safe=true;
     }
 }
 
 
 function OnTriggerExit (myTrigger : Collider) {
     if((myTrigger.gameObject.name == "FPSController")) {
         player.GetComponent(Death).safe=false;
     }
 }

The trigger collider is slightly smaller then the FPScontroller but I have tried to make a smaller collider inside the FPScontroller that the safe area trigger looks for which didn't solve the problem. Please help! I'm going insane trying to understand why Unity is doing what its doing!

Edit: I'll try to describe the problem more clearly. The player walks around and an earthquake will randomly hit. The player then needs to get to a safe area to live. The problem I'm having is that when the player enters the safe area the timer continues to run and the player dies (then all hell breaks loose and unity gives off tons of errors. Errors that don't make sense like can't find guiSkin or gameobjects that are attached to the script even tho it was just using them before the player died without a problem.)

I thought the problem was the trigger is too small so the player is entering and exiting at the same time which wasn't the case. I even commented out the set safe false part and somehow magically it would still flip between true/false rapidly. I've checked all my other scripts to make sure that these two are the only ones effecting this variable. I know it was flopping back and forth because I put the variable on the screen with a GUI label. In the inspector it said it was true like it should. But the GUI label had both true and false overlapped on eachother. Maybe I'm wrong but that tells me its flipping back and forth like every update.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RyanZimmerman87 · Aug 14, 2013 at 03:24 AM

I'm not sure if I fully understand the problem and I've never really tried too much stuff in the web player.

But the first thing I would do is declare the gameObject variable for retrieving the "FPSController". I don't think you would want to use the .Find command every time you have a trigger event it should be better to already have it set up in the Start() function.

I think if you do that you could possibly fix this problem hard to say though. This is just a guess but I'm thinking maybe since you find the object every time it is causing some strange behavior instead of always using the one declared variable.

You also shouldn't have to use MyTrigger.gameObject.name at least in C# you can just do MyTrigger.name and that is the gameObjects name with that trigger.

So if it was C# it would look more like this for your code that I suspect could be the problem:

 GameObject playerObject;
 
 void Start()
 {
 //not sure where this script is attached too?
 
 //if on the FPSController this should work
 playerObject = gameObject;
 
 //if on some other object
 playerObject = GameObject.Find ("your player object name");
 
 //or just use a public variable and manually assign the playerObject in inspector is probably even easier
 }
 
 void OnTriggerEnter(Collider triggerObject)
 {
 if (triggerObject.name == "your player object name")
 {
 PlayerObjectScript otherObjectsScriptPlayer = playerObject.getComponent<PlayerObjectScript>();
 
 otherObjectsScriptPlayer.safe = true;
 }
 {
 
 void OnTriggerExit(Collider triggerObject)
 {
 if (triggerObject.name == "your player object name")
 {
 PlayerObjectScript otherObjectsScriptPlayer = playerObject.getComponent<PlayerObjectScript>();
 
 otherObjectsScriptPlayer.safe = false;
 }
 {
 

That's just a guess sorry for C# example. It seems like this should work fine unless there is something going funky with the Web Player.

Comment
Add comment · Show 3 · 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 Jenkins6161 · Aug 14, 2013 at 06:26 PM 0
Share

Did as you said and still have the same problem.

avatar image RyanZimmerman87 · Aug 16, 2013 at 11:09 PM 0
Share

You do have all the variables initialized in the Start() function right?

I'm not sure if I can provide further assistance it seems like it should work unless there is something about your project that is not compatible with the Web Player since you said it works in other platforms right?

avatar image Jenkins6161 · Aug 19, 2013 at 11:58 PM 0
Share

I ended up rewriting some things and moving stuff around. It works now. Was most likely just some shotty code that was doin weird stuff.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Varaible not decementing OnTriggerExit 1 Answer

OnTriggerEnter, OnTriggerExit Work Only Once 2 Answers

OnTriggerExit happens too soon! 1 Answer

Display gui box after trigger is activated? 2 Answers

Check if object is coliding with another in a function 0 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