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 Althaen · Nov 01, 2012 at 05:51 AM · javascripttoggleweapon

How to toggle between 3 different settings

What I am trying to do is make pressing Z on the keyboard toggle between 3 values. I cannot figure out how I would do this.

So:

value is 1 > Z is pressed > value toggles to 2 > Z is pressed > value toggles to 3 > Z is pressed > value toggles back to 1

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

5 Replies

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

Answer by XienDev · Nov 01, 2012 at 09:56 AM

 using UnityEngine;
 using System.Collections;
 
 public class first : MonoBehaviour {
     
     private int z = 1;
     
 
     void Update () {
         
         if (Input.GetKeyDown(KeyCode.Z))
         {
             z = (z % 3) + 1;
             print (z);
         }
     
     }
 }
 
Comment
Add comment · Show 9 · 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 Althaen · Nov 02, 2012 at 12:05 AM 0
Share

It stops working once it gets back to 1.

avatar image XienDev · Nov 02, 2012 at 06:45 AM 0
Share

check my snippet.

avatar image Althaen · Nov 02, 2012 at 10:55 PM 0
Share

Perhaps I'm missing something because I'm not using C, I'm using javaScript.

$$anonymous$$aybe I should just switch to C...

avatar image Althaen · Nov 02, 2012 at 11:18 PM 0
Share

Nope still does not work... I press Z and it prints 2, then 3, then 1, but then it stops printing after that. No idea what I'm doing wrong.

avatar image Dave-Carlile · Nov 02, 2012 at 11:24 PM 0
Share

Is the object getting destroyed? If that code executes then it will always cycle between 1 and 3.

Show more comments
avatar image
1

Answer by XienDev · Nov 01, 2012 at 09:48 AM

z = (z % 3) + 1;

and don't forget to make it 1; at default

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 Althaen · Nov 02, 2012 at 12:04 AM 0
Share

Thank you that worked but the only problem is that it stops working once it gets back to 1. I want it to continue to cycle through whenever Z is pressed.

avatar image
0

Answer by suresh.murugesan · Nov 01, 2012 at 06:11 AM

check the time difference between the first and next button . you can solve it easily ... Time.deltaTime use this..

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 Griffo · Nov 01, 2012 at 08:22 AM

Try this, but I don't think Fattie will like it (else if) .. ;)

 #pragma strict
 
 private var onePress : boolean = true;
 private var twoPress : boolean = false;
 private var threePress : boolean = false;
 
 function Start () {
 
 }
 
 function Update () {
 
 if (Input.GetKeyDown ("z")){
     keyPress();
     }
 }
 
 function keyPress(){
 
  if (onePress && !twoPress && !threePress){
     onePress = false;
     twoPress = true;
     threePress = false;
 // Do Stuff
     print("Button press 1");
  }else if (!onePress && twoPress && !threePress){
      onePress = false;
      twoPress = false;
      threePress = true;
 // Do Stuff
     print("Button press 2");
  }else if (!onePress && !twoPress && threePress){
      onePress = true;
      twoPress = false;
      threePress = false;
 // Do Stuff
     print("Button press 3");
      }
 }
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 FLASHDENMARK · Nov 01, 2012 at 09:58 AM 0
Share

I wouldn't like it either ;). Not because of the else if's but because it would be easier changing a integer value :)

avatar image
0

Answer by Althaen · Nov 03, 2012 at 08:30 AM

I found out why it stops at 1...

The code:

z = (z % 3) +1;

It only works 3 times. If I set it to 5 it works 5 times. If I set it to 57 it works 57 times.

How do I make it go between 1-3 forever?

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

15 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

Related Questions

Weapon pick up and switching script 2 Answers

Weapon swapping in-game 2 Answers

Could anyone help me with .js code to be able to pickup a flashlight and turn it on and off -1 Answers

making a named script update 1 Answer

Help With Remote Explosive 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