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 Conect11 · Sep 13, 2013 at 12:50 AM · cameraif-statements

Using the same controller button twice

I'm so close with this I can taste it. I think.

I'm using a camera switch script:

 var camera1 : Camera;
  
 var camera2 : Camera;
  
  
  
  
 function Start () {
  
 camera1.camera.active = true;
  
 camera2.camera.active = false;
  
  
 }
  
  
 function Update () {
  
 if(Input.GetButtonDown("Inventory")){
  
 camera1.camera.active = false;
  
 camera2.camera.active = true;
  
  
 }
  
  
  
 if(Input.GetKeyDown("l")) {
  
  
 camera2.camera.active = false;
  
 camera1.camera.active = true;
  
  
 }
  
  
 }

What I'd like to do is use the same button to enable and disable it. (essentially I'm using it to switch between my main camera, and a menu) Right now "Inventory" takes you into it, but "l" takes you out. Reason being, if I assign both to "Inventory" then nothing happens. Now, I suspect that all that's needed is a simple "if" statement, such as:

 if({camera2.camera.active = true;)

but being so new, I'm not sure if that's

A: Correct, or B: Where I should put it anyway.

Each effort has resulted in errors so far, parse, unexpected token, etc. If someone might be kind enough to help me figure this out, I'd be grateful. God bless.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RyanZimmerman87 · Sep 13, 2013 at 04:57 AM

         public Camera camera1;
          
         public Camera camera2;
          
         bool camera1ActiveBool;
          
          
         void Start () {
          
         camera1.camera.active = true;
          
         camera2.camera.active = false;
          
         camera1ActiveBool = true;
         }
          
          
         function Update () {
          
         //use whatever button you want to toggle
         if(Input.GetKeyDown("l")) {
          
     if (camera1ActiveBool == true)
     {
     camera1.camera.active = false;
     camera2.camera.active = true;
 
 camera1ActiveBool = false;
     }
     
     else if (camera1ActiveBool == false)
     {
     camera1.camera.active = true;
     camera2.camera.active = false;
 
 camera1ActiveBool = true;
     }
           
         } 
         }
 
 

 
Comment
Add comment · Show 7 · 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 RyanZimmerman87 · Sep 13, 2013 at 05:02 AM 0
Share

I am assu$$anonymous$$g all your other syntax is correct all I did was swap the variables to C# since I don't use JavaScript but just set up a bool like I did and it's very easy.

Also did you remember to attach the camera1 and camera2 variables in the Unity Inspector of this script? That is probably the problem with the error you posted. You should have 2 cameras in your scene, you need to drag each one into the correct place within the script in the Unity Inspector.

Apologies for formatting editing this thing can be a pain...

avatar image Conect11 · Sep 13, 2013 at 05:10 AM 0
Share

Hey Ryan,

Thanks for putting that together, will definitely check it out. The two cameras were both in the scene, main camera on player, pause menu camera high in the clouds. Here's the output of that camera:

Pic

As long as I keep the "enter picture" and "exit picture" (not actual names, just my examples) keys as separate keys, then everything is fine and works great. It's just when I was trying to make them one key that I had the issue.

EDIT: Once again, fantastic work Ryan, thanks! I changed one function to a void and everything ran swim$$anonymous$$gly. =) Quick question: After my initial post I added a pause component, since, you know, getting killed while you're in a menu would suck. Not sure how to implement it in C# though. Here's the code (with pause) in js:

EDIT 2: NV$$anonymous$$, a little Time.timescale action did the trick =)

     var camera1 : Camera;
      
     var camera2 : Camera;
      
      
      
      
     function Start () {
      
     camera1.camera.active = true;
      
     camera2.camera.active = false;
     
     
      
      
     }
      
      
     function Update () {
      
     if(Input.GetButtonDown("Inventory")){
      
     camera1.camera.active = false;
      
     camera2.camera.active = true;
     
     paused = true;
    
    Time.timeScale = 0;
      
      
     }
      
      
      
     if(Input.Get$$anonymous$$eyDown("l")) {
      
      
     camera2.camera.active = false;
      
     camera1.camera.active = true;
      
      paused = false;
    
    
    Time.timeScale = 1;
      
     }
      
      
     }
avatar image RyanZimmerman87 · Sep 13, 2013 at 11:45 PM 0
Share

Glad you got it working! All you need is the Time.timeScale to pause the enemies in the Update and FixedUpdate.

But just one more tip is that you may find that paused bool to eventually be very useful. Depending on how complex your project gets you could set a public static paused variable and use it to disable multiple scripts simultaneously, deter$$anonymous$$e which GUI components to show or not show etc (GUI components can have some nasty side effects even if you can't see the underlying problem behind the new GUI).

So for example if you have a pause menu that fills the whole screen but you still have some buttons on the regular game-play screen, you will need to disable those game-play buttons even if you can't see them currently.

This might help some people new to using GUI's in multiple scripts together, or setting up somewhat complex GUI options menus with many different screens for the first time.

avatar image Conect11 · Sep 13, 2013 at 11:50 PM 0
Share

learned that even using a controller there are some weird button quirks. While I can't walk, I can still look around, lol.

avatar image RyanZimmerman87 · Sep 13, 2013 at 11:52 PM 0
Share

If you can't walk it sounds like your timeScale was not reset. Are the enemies not able to move at this time either?

Show more comments
avatar image
0

Answer by sooncat · Sep 13, 2013 at 02:04 AM

 camera.active = !camera.active;
Comment
Add comment · Show 5 · 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 Conect11 · Sep 13, 2013 at 04:41 AM 0
Share

Thanks for the answer, Soon, but unfortunately no dice, so far. I've no doubt it's user (me) error. The best I've gotten is this error:

$$anonymous$$issingComponentException: There is no 'Camera' attached to the "Player" game object, but a script is trying to access it. You probably need to add a Camera to the game object "Player". Or your script needs to check if the component is attached before using it. CameraSwitch.Update () (at Assets/Standard Assets/Scripts/CameraSwitch.js:30)

avatar image MrProfessorTroll · Sep 13, 2013 at 04:42 AM 0
Share

Where exactly did you put that piece? That should work

avatar image Conect11 · Sep 13, 2013 at 04:56 AM 0
Share

like I said, user error :)

about 3 _ 4 different places,including:

after the two camera variables (line 5ish) after function start (abt line 9) after each getinput statement, both together and separately.

$$anonymous$$d you, I added no brackets, but didn't get any game breaking errors, either.

avatar image sooncat · Sep 13, 2013 at 05:39 AM 0
Share

looks like grammar error. my answer a hint not the code.

avatar image Conect11 · Sep 13, 2013 at 05:43 AM 1
Share

no worries at all, I appreciate the valuable $$anonymous$$ching tool you gave me. "Teach a man to fish." Am grateful for people like you, Ryan, and others who have so generously helped me either with code, or pointing me in the right direction to continue learning.

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

18 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

Related Questions

Js to c# conversion 1 Answer

Object will not instantiate 1 Answer

Create a if statement using GetComponent? 1 Answer

Error with Camera WorldtoScreenPoint 1 Answer

Trying to implement an if or statement 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