Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TheFrankman123 · Apr 26, 2012 at 09:55 PM · ontriggerenteractivedeactivate

Turing GameObjects On and Off again.

Hello, I am currently creating a puzzle that when you click a button it opens certain pathways but closes others. I also need the player to be able to press the button again and switch it back so here is what i am currently working with.

 var pressed : boolean = false;
 var laser1 : GameObject;
 var laser4 : GameObject;
 
 function OnTriggerEnter (button : Collider) {
      if(button.gameObject.tag == "Player" && pressed == false) {
     laser4.active = false;
     laser1.active = true;
     pressed = true;
     }
 
     if(button.gameObject.tag == "Player" && pressed == true) {
     laser4.active = true;
     laser1.active = false;
     pressed = false;
 
 }

So, the idea is that when they enter the trigger it makes one laser active and the other one false, then sets the pressed boolean to true so that when you enter the trigger again it will recognize that the boolean is true and turn them back to how they were.

However this doesn't work, since i'm sure that the second that you enter the trigger it's doing the function for when pressed is true, then because it's changed to true does the function for pressed being true straight away...

Can anyone help?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by dannyskim · Apr 26, 2012 at 10:09 PM

Well, first glance at your logic, if pressed initialized as false, let's take a look:

 if(button.gameObject.tag == "Player" && pressed == false) {
 laser4.active = false;
 laser1.active = true;
 pressed = true;
 }

It goes into this code block above, you switch the 'lasers', and then set pressed back to true. So, when the code block goes to the next if statement below:

 if(button.gameObject.tag == "Player" && pressed == true) {
 laser4.active = true;
 laser1.active = false;
 pressed = false;
 }

it's going to execute that as well because the previous if block already changed pressed to true.

Comment
Add comment · Show 2 · 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 TheFrankman123 · Apr 26, 2012 at 10:11 PM 0
Share

Yeah, i figured that was the problem. So i tried only switching the pressed boolean on trigger exit but that does the same thing... What's a different way of doing its?

avatar image Lo0NuhtiK · Apr 27, 2012 at 10:51 AM 0
Share

Try this...


    if(button.gameObject.CompareTag("Player")){
       laser4.active = pressed ;
       laser1.active = !pressed ;
       pressed = !pressed ;
    }

or use an if/else


    if(button.gameObject.CompareTag("Player")){
       if(pressed){
          laser4.active = true ;
          laser1.active = false ;
          pressed = false ;
       }
       else{
          laser4.active = false ;
          laser1.active = true ;
          pressed = true ;
       }
    }
avatar image
0

Answer by TheFrankman123 · Apr 26, 2012 at 10:37 PM

Right, Got it. The problem was as 'dannyskim' stated. So the correct way of doing it is like this:

 var pressed : boolean = false;
 var laser1 : GameObject;
 var laser4 : GameObject;
 
 function OnTriggerEnter (button : Collider) {
      if(button.gameObject.tag == "Player" && pressed == false) {
          laser4.active = false;
          laser1.active = true;
          pressed = true;
     
     } else {
          laser4.active = true;
          laser1.active = false;
          pressed = false;
     }
 
 }
Comment
Add comment · Show 2 · 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 TheFrankman123 · Apr 26, 2012 at 11:20 PM 0
Share

Actually I take this answer back... it's rubbish way of doing it.

avatar image Lo0NuhtiK · Apr 27, 2012 at 11:02 AM 0
Share

wtf is going on with this page? all of these other posts weren't displayed earlier (or the last few times I clicked into this topic) after/before I posted my comment earlier. ... The one by @daringly wasn't displayed just a second ago when I left, then came right back in here now and can see it.

avatar image
0

Answer by daringly · Apr 26, 2012 at 11:26 PM

Yes, this seems like a problem, because if something else hit the collider and triggered it, the "else" would be executed. You could always make it an "else if" and do a player tag check.

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 TheFrankman123 · Apr 27, 2012 at 10:35 AM

I the problem with this is that if say you added a button click into it. It would change the boolean to true/false whenever you walked into the trigger area regardless of whether you pressed the button or not. Meaning that if you walked through the trigger area and didn't press the button, then next time you pressed the button it would behave the wrong way.

Another issue is that when you put a button press in to activate it for some reason it doesn't work the first time you enter the trigger... very strange behavior.

They way i have resolved this when you enter the trigger and it changes which objects are one and off it adds a red glow around the trigger area to show it's activation.

This is not really a solution since it only works in the way my game functions and the puzzle you are completing.

Would be good to see a proper solution though.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Referencing children of an instantiated object. 1 Answer

Trigger Enter Issue 1 Answer

How can I enlarge the box collider when a prefab is active 1 Answer

Can Someone help me with this script? 1 Answer

Having a game object de-activate itself 1 Answer


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