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
1
Question by death · Oct 31, 2014 at 10:38 PM · arrayoperator

Questions about a Weapon Switch script.

     using UnityEngine;
     
     public class SwitchMethodOne : MonoBehaviour {
     
     public GameObject currentWeapon;
     public GameObject[] myWeapons = new GameObject[]
     public int currentWeaponIndex = 0;
     
     public void Awake() {
     myWeapons = new GameObject[Enum.GetValue(typeof(myWeapons)).Length];
     }
     
     void Update() {
     if(Input.GetButtonDown("KeyCode.LeftShift")) {
     currentWeaponIndex = (currentWeaponIndex + 1) % myWeapons.Length;
     currentWeapon = weaponArray[currentWeaponIndex];
 


Questions:

What is the purpose of "(currentWeaponIndex + 1) % myWeapons.Length;?"

  . Currently, "currentWeaponsIndex" is equal to 0, so why are we just adding 1 to it?

(In reference to first question) why are we getting the remainder of 1/myWeapons.Length? . "% myWeapons.Length;"

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
2
Best Answer

Answer by Habitablaba · Oct 31, 2014 at 10:41 PM

What is happening here, is a bit of error checking and defensive programming. It makes it so the user cannot select a weapon out of range of the weapon collection, instead it loops around the list. So if the user is at the end of the list and presses shift, they go to the first weapon.

The mod operator (%) is a great way to make sure a value stays within the bounds of another value.

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 death · Oct 31, 2014 at 10:48 PM 0
Share

So is it safe to say that for question 1, the (%) is like saying "of?"

Increment up until you reach the limit of myWeapons.Length

And I thought (%) meant take the remainder of the two values you're diving. So say we have two guns, would it be 1/2?

avatar image Habitablaba · Oct 31, 2014 at 11:02 PM 0
Share

it is taking the remainder! But it is also being super awesome. Check it out:

assume length = 3 and current index = 0;

[press shift]
0 + 1 = 1
1 / 3 = 0, R 1
index = 1;

[press shift]
1 + 1 = 2
2 / 3 = 0, R 2
index = 2;

[press shift]
2 + 1 = 3
3 / 3 = 1, R 0
index = 0; // this is the cool part. Now we're back at the start.

it has the added benefit of a bit of error checking. Let's assume index gets all crazy and out of bounds.
assume length = 3 and current index = 4;

[press shift]
4 + 1 = 5
5 / 3 = 1, R 2
index = 2; // neat!

avatar image death · Oct 31, 2014 at 11:13 PM 1
Share

Thanks! I understand now :D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Wrong object always Instantiated 1 Answer

Read different game objects into array at same time? 2 Answers

How to make an array list set to false gradually instead of instantly? 2 Answers

Multidimensional arrays - editing one element within one of the arrays 1 Answer

rotating vertices with for loop in 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