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 skylerkingdev · Oct 27, 2012 at 11:05 PM · arrayloopfor

Adding an object to an array only if it is not in the array

I am trying to add and object to an array. I am trying to check if the object is already in the array, if it isnt, then add it. If the object is in the array then I dont want to add it. The issue that I am running into, is that my script continuously adds the object to the array. Here is what I have so far :

 for ( var i : int = 0; i < activearray.Length; i++ )
                 {
                 var selectedobject : GameObject = activearray[i];        
                 if ( selectedobject == hit.collider.gameObject) 
                 {
                               Debug.Log("tile is already grouped");
                        } 
                        else 
                        { 
                         activearray += [hit.collider.gameObject];
                        }
                    }
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
0

Answer by dannyskim · Oct 27, 2012 at 11:16 PM

Unityscript Javascript arrays are slow, as far as I have read.

It's better to just simply use a list for this functionality, because it already has a method to check if an object exists in the list.

 if( yourList.Contains( hit.collider.gameObject ) )
     Debug.Log("Tile is already grouped");
 else
     yourList.Add( hit.collider.gameObject );

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

remember to add System.Collections.Generic to your import / using statements.

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 Eric5h5 · Oct 27, 2012 at 11:22 PM 0
Share

The code isn't using a Javascript array, it's a GameObject[] array.

avatar image
0

Answer by Eric5h5 · Oct 27, 2012 at 11:21 PM

Imagine that you have an array of 10 entries, and the object in question is one entry in the array. Your code will loop through the array and add hit.collider.gameObject for every entry that's not the same game object, so it will be added 9 times.

You should use List instead of an array if you're adding items to a collection. In that case you can just use List.Contains to see if the object is in the List or not.

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 skylerkingdev · Oct 27, 2012 at 11:43 PM 0
Share

It seems to still add to the list continuously. What am I missing here?

function CastRay() { var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit; var groupedTiles = new List.();

     if(Physics.Raycast(ray, hit)) 
     {
        if(!activeobject) 
        {
            activeobject = hit.collider.gameObject; 
            groupedTiles.Add(hit.collider.gameObject); 
        }
        else if (hit.collider.gameObject.layer == activeobject.layer && !groupedTiles.Contains(hit.collider.gameObject)) 
        {            
                     groupedTiles.Add( hit.collider.gameObject ); 
                     numTiles +=1;
                     Debug.Log("tile added to group");
            }
       
      }

}

avatar image skylerkingdev · Oct 27, 2012 at 11:49 PM 0
Share

Actually, moving the declaration for the list outside of the function seems to have cleared that up. Thank Eric, you are extremely helpful. I guess my other question is how do I iterate through a list if it doesn't have length?

avatar image dannyskim · Oct 28, 2012 at 12:04 AM 0
Share

...List.Count, and you would simply access the object the same way as a built-in array, simply use the index number.

avatar image skylerkingdev · Oct 28, 2012 at 12:04 AM 0
Share

Ah, Count, thank you! It still seems a little strange to me .. a function like this

function CheckGroup() { for (var i=0; i < groupedTiles.Count; i++) { score+=1; groupedTiles.RemoveAt(i); } }

isnt actually clearing through the whole list. It works fine for one object, but if you do four or more, it clears only 2.

avatar image Eric5h5 · Oct 28, 2012 at 02:15 AM 0
Share

Because you're increasing i by 1 every time through the loop after removing the element at i. You can just use List.Clear to clear all items.

Show more comments

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

11 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

Related Questions

[Solved]Using multiple arrays in one statement? 1 Answer

How to correctly shorten this script using arrays and iterations 2 Answers

how do i make a loop for a boolean? 2 Answers

I dont know why im getting a null reference exception 1 Answer

How can I move back to Element 0 in an array to reset a loop? 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