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 xToxicInferno · Jun 08, 2010 at 09:52 PM · arrayarrayscompare

Compare Contents of arrays

Alright the problem here is i want my array to be a int[]. The problem is I do not know how to compare one number to all the contents of a array. Here is my code:

var randomNum : int; var randomNum2 : int; var compareNumber : int[]; var constantNum : int = 0; var once : boolean = true; var brick : Transform; var rowOne : Transform[]; var rowTwo : Transform[];

function Start () {

}

function Update () { if (once) { randomNum = Random.Range(1,5); if (randomNum > 0) { if(randomNum2 == compareNumber) { randomNum2 = Random.Range(1,rowOne.length); compareNumber[constantNum] = randomNum2; Instantiate(brick, rowOne[randomNum2].transform.position, Quaternion.identity); constantNum++; } else { randomNum2 = Random.Range(1,rowOne.length); } randomNum--; } once = false; } }

The problem is the line: if(randomNum2 == compareNumber) {...well i KNOW it is wrong, thus the problem. What i want is IF the 2nd random number IS in the array it picks a new random number. The correct syntax (at least what i want) is:

if(randomNum2 !== compareNumber) {

The problem is that compareNumber is a int[] so i can not see if it not equal to it, or so unity says. So how do i got about having it compare to the contents of the array? Sorry for this sort of question, i am new to arrays and i want to start using them because they are just so useful, just complex i guess.

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

Answer by Tetrad · Jun 08, 2010 at 10:16 PM

http://msdn.microsoft.com/en-us/library/d9hy2xwa(v=VS.100).aspx or http://msdn.microsoft.com/en-us/library/7eddebat(v=VS.100).aspx

So something like

if( System.Array.IndexOf( compareNumber, randomNum2 ) == -1 )
{
    // it doesn't exist in the array
}
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 xToxicInferno · Jun 08, 2010 at 10:20 PM 0
Share

I get a error saying that IndexOf is not a member of Array.

avatar image Mike 3 · Jun 08, 2010 at 10:29 PM 0
Share

Use System.Array.IndexOf so it doesn't mix it up with UnityEngine.Array

avatar image Tetrad · Jun 13, 2010 at 10:28 AM 0
Share

Edited my post so it should compile as is

avatar image
0

Answer by Noise crime · Jun 09, 2010 at 01:52 AM

Maybe i'm reading it wrong and it is late, but even if you get the compare working is this code correct? I ask because it doesn't seem to make a lot of sense to me.

Your first time through 'once' is true, so it generates a positive random number, which means the conditional check is always true. You then want to compare randomNum2 against your array, but according to the code you posted, its yet to be initialise properly, though i'm not sure if JS will default it to a value or null. If its null i'd expect the compare to error out.

Next you create a completely new randomNum2 in the range of the number of transforms you've added to rowOne, which seems odd considering you were checking to make sure the original randomNum2 wasn't in the previous used array.

Then you assign the new randomNum2 to the next (constanNum) index in the compareNumber array, presumably in order not to select it again. However since its no longer the same randomNum2 as you did the compare with you have no gaurantee that it is a number that hasn't previously been used.

The new randonNum2 is then used as an index to choose a 'random' transform from your array, before incrementing constantNum.

Finally you decrement randonNum by one, as though you want to generate up to the original randonNum 'bricks', yet it is irrelevant as next time into update() it will be recalculated anyway, but that doesn't matter as lastly you set 'once' to false so this chunk of code want be called again at all next time through anyway.

What is it exactly that you want to achieve here? I don't know maybe the code makes sense if there are other functions, but it looks odd to me. If you want to instantiate a random number of bricks with random transforms then there are easier ways.

For example, create a random number (randNUM ) to determine how many bricks to initiate. Use this value to build an array with consecutive numbers from 0 to (randNUM -1). Then iterate over the array using a random number as the index (range would initially be 0 to randNUM -1), take the value at that index and push it into a new array, delete the original value from the original array and decrease the random range by 1 for the next random index.

That way you can build a random array of index values from 0 to randonNumber -1.

Then its simply a case of iterating through the array using the values as indices into your rowOne array to get random transforms.

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 xToxicInferno · Jun 09, 2010 at 02:13 AM 0
Share

Yeah i noticed this problem after i tested it, so i had to move some declaring variables around. Though i got a whole heap of problems as i do not know how to code the next part lol.

avatar image Noise crime · Jun 09, 2010 at 11:58 AM 1
Share

Well as I said, why not explain what you are trying to achieve, that way we can better address you code issues.

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

No one has followed this question yet.

Related Questions

Having Multiple Controllable FPS Units Selected From A Singular RTS Mode? 0 Answers

Check/compare array elements 1 Answer

Array Within Index 1 Answer

my QandA array is not working when you choose 3 wrong answers 1 Answer

Array empties values on RunTime? 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