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 /
This question was closed Aug 26, 2012 at 11:18 PM by Chimera3D for the following reason:

Other

avatar image
0
Question by Chimera3D · Aug 26, 2012 at 09:04 PM · functionarrayscallonceresizing

Calling A Function Once

I have a few functions that can resize Unity's built-in array system, however I'm having a problem where the elements are added repeatedly, I just the element to be added once. The answer to this might be simple, but I can't figure anything out. I tried using an if statement to test a variable that checks if the element has been added but this doesn't work very well, in addition I don't want to have to make a new variable for every time I want to use this function. I also have a remove function, I'll post that as well in-case the functions need specific ways to do this.

The Add Function

 static function AddGO(arr : GameObject[], obj : GameObject)
 {
     
     var tmpArr : GameObject[];
     tmpArr = arr;
     arr = new GameObject[tmpArr.length +1];
     arr[arr.length -1] = obj;
     
     for(i = 0; i < arr.length -1; i++)
         arr[i] = tmpArr[i];
     return arr;
 }


The Remove Function

 static function RemoveGO(arr : GameObject[], obj : GameObject)
 {
     var index = -1;
     for (var i = 0; i < arr.length; i++)
     {
         if (arr[i] == obj)
         {
             index = i;
             break;
         }
     }
     if (index == -1) // the element wasn't found
         return arr;
     var tmpArr = new GameObject[arr.length - 1];
     for(i = 0; i < index;i++)
         tmpArr[i] = arr[i];
     for(i = index+1; i < arr.length;i++)
         tmpArr[i-1] = arr[i];
     return tmpArr;
 }

What I'm using to call them:

modArray = array; modElement = element; modArray = AddGO(modArray, modElement); array = modArray;

Removing is the same thing but just with Remove instead of Add.

Comment
Add comment · Show 9
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 · Aug 26, 2012 at 09:47 PM 0
Share

If you call a function once, it's only called once; nothing will make it be called multiple times unless you do that yourself. You should really use List ins$$anonymous$$d of doing this though. If you absolutely have to resize a built-in array for some reason, then use System.Array.Resize.

avatar image Chimera3D · Aug 26, 2012 at 09:49 PM 0
Share

the code I'm using to call it is inside of a function thats called from an update function

avatar image Eric5h5 · Aug 26, 2012 at 09:54 PM 0
Share

Well, if it's in Update then I guess it's being called every frame? In that case you don't want to use Update, or at least add some logic to prevent it being called every frame.

avatar image Chimera3D · Aug 26, 2012 at 09:58 PM 0
Share

That's the problem, this is called many times and making logic for it being called every time is a bit tedious (as in every time I want to add an element). The reason I don't want to use lists is because I need this to be as fast as possible.

avatar image Eric5h5 · Aug 26, 2012 at 10:36 PM 1
Share

You're not gaining any speed by using a GameObject array ins$$anonymous$$d of a List of GameObjects; in fact you're losing speed whenever you add/subtract, because List can do this much more efficiently than this code.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What's wrong with this code? 1 Answer

Calling a function only once in Update 3 Answers

Efficient way for calling a function once 3 Answers

Calling Functions from another Script 3 Answers

Call Function with Collision-parameter 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