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 /
avatar image
0
Question by emir3100 · Nov 25, 2016 at 07:56 AM · lightkeycodeelseelse if

Can't use KeyCode multiple times

Im making a light switch code, so when I go the the trigger the text pops up (Press E to use) and when I press E then the light turns on but I cant use E again in the (else if) I have to use other key code to make it work. Here is the script, thanks

 using UnityEngine;
 using System.Collections;
 
 public class LightSwitch : MonoBehaviour {
 
     public GameObject light;
     
     public bool activateTrigger = false;
     public bool T_ActivatedOpen = true;
     public bool T_ActivatedClose = false;
     public bool Press = false;
 
 public GameObject textO;
     public GameObject textC;
 
 
 void Start () {
     textC.SetActive(false);
     textO.SetActive(false);
     T_ActivatedOpen = true;
     T_ActivatedClose = false;
     light.SetActive(false);
 
     }
     
 
     void Update () {
 
 
         if (T_ActivatedOpen == true)
         T_ActivatedClose = false;
 
     else if (T_ActivatedClose == true)
         T_ActivatedOpen = false;
 
 
     if (activateTrigger && Input.GetKey(KeyCode.E))
     {
         textO.SetActive(false);
         textC.SetActive(false);
         T_ActivatedOpen = false;
         T_ActivatedClose = true;
         textO.SetActive(false);
         textC.SetActive(true);
         light.SetActive(true);
 
             if (light == true)
                 textC.SetActive(true);
             textO.SetActive(false);
         }
            else  if (activateTrigger && Input.GetKey(KeyCode.F))
         {
             light.SetActive(false);
             textO.SetActive(true);
             textC.SetActive(false);
 
             if (light == false)
                 textC.SetActive(false);
             textO.SetActive(true);
         }
 
     }
 
 
 void OnTriggerEnter(Collider col) 
     {
         if (col.gameObject.tag == "Player")
         {
 
             activateTrigger = true;
             if ((T_ActivatedOpen == true))
                 textO.SetActive(true);
 
             if ((T_ActivatedClose == true))
                 textC.SetActive(true);
         }
 
     }
 
 
     void OnTriggerExit(Collider col) 
     {
         if (col.gameObject.tag == "Player")
         {
             textO.SetActive(false);
             textC.SetActive(false);
             activateTrigger = false;
         }
 
     }
 }

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 Bunny83 · Nov 25, 2016 at 08:48 AM

Uhh, this is pure chaos ^^ I think you fiddled too long on this script. You have tons of redundancy and nonsense all over the place.

Here, i rewrote the script:

 using UnityEngine;
 using System.Collections;
 
 public class LightSwitch : MonoBehaviour
 {
     public GameObject light;
     public GameObject textO;
     public GameObject textC;
 
     void Start()
     {
         textC.SetActive(false);
         textO.SetActive(false);
     }
 
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
             textO.SetActive(light.activeSelf);
             textC.SetActive(!light.activeSelf);
         }
     }
     void OnTriggerExit(Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
             textO.SetActive(false);
             textC.SetActive(false);
         }
     }
     void OnTriggerStay(Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 // toggle the light. If off turn it on,  if on turn it off
                 light.SetActive(!light.activeSelf); 
                 // update the texts based on the new active state.
                 textO.SetActive(light.activeSelf);
                 textC.SetActive(!light.activeSelf);
             }
         }
     }
 }

Major changes:

  • OnTriggerEnter is called every frame while something is inside the trigger. So we don't need Update at all

  • I used GetKeyDown instead of GetKey. GetKeyDown only returns true for one frame when you press the key down. GetKey returns the "state" of the key so when you hold the key down it will return true every frame.

  • No need for all those variables. The light gameobject has it's active state which you want to toggle in the first place, so you can simply read it back when you want to know the current state.

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 emir3100 · Nov 25, 2016 at 09:53 AM 0
Share

Thank you very much

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need Help Improving If Else statement 1 Answer

"Else" not working 2 Answers

How to use multiple else if statements C# 2 Answers

VertexLit particles not affected on all directions 0 Answers

Why do Unity 5 is glitched as hell? Please, I need help... 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