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 santi5655 · Apr 09, 2013 at 08:27 PM · javascriptarrayintconditionequal

Check if an int is equal to an array int

i want to do something if my integer is equal to any element of an int array using this variables:

 var myInt : int;
 var myIntArray : int[];
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MartinCA · Apr 09, 2013 at 08:39 PM

Just iterate over the entire array and exit with success as soon as you find a match, otherwise return no match is found. JS Arrays should also have this functionality built in.

 Function FindInArray( needle : int, haystack : int[] )  : bool
 {
    for each ( var item : int in haystack )
    {
       If ( needle == item )
          Return true;
    }
 
    Return false;
 }

Mind you, this is pseudo JS, I am writing from my ipad and my knowledge of the syntax is kinda rusty - so don't expect a copy paste to work. But this should give you a general idea.

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 Dracorat · Apr 09, 2013 at 08:42 PM 0
Share

if(myIntArray.indexOf(myInt) > -1)

That's the usual way to do it in Web javascript - I imagine it'd be the same in Unity javascript.

avatar image Eric5h5 · Apr 09, 2013 at 09:38 PM 0
Share

Unity Javascript isn't at all like web Javascript though, which is why it's usually referred to as Unityscript. Also built-in arrays are part of $$anonymous$$ono, specifically System.Array. So anything to do with arrays (not the built-in Unity Array class, which shouldn't be used anyway) can be looked up in the $$anonymous$$SDN docs under System.Array. There's never any need to write your own code for basic functionality like this.

avatar image
0

Answer by whebert · Apr 09, 2013 at 09:31 PM

Not sure if you are stuck using an array, but you could always use generic lists instead - gives you dynamic arrays and some easy to use functionality, including an easy way to see if the list contains something.

 import System.Collections.Generic;
 
 
 var myIntList : List.<int> = new List.<int>();
 
 // Add your ints
 myIntList.Add(1);
 myIntList.Add(2);
 myIntList.Add(3);
 
     
 if(myIntList.Contains(myInt))
 {
     // The list contains your int
 }
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 Eric5h5 · Apr 09, 2013 at 09:35 PM

Use IndexOf, which returns -1 if the item is not found.

 if (System.Array.IndexOf (myIntArray, myInt) != -1) {
     // it's here yo
 }
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

14 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

Related Questions

Arrange ints from biggest to smallest and display them in a table 1 Answer

JS: Array index not taking array.length as valid int? 2 Answers

Number parse 1 Answer

Is there a decent tutorial for a local high score table (android)? 1 Answer

Error when debuging array elements name 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