Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by BooberSmack · Dec 20, 2016 at 03:25 PM · strange ioc

Creating constant Function to array elements that are continuous. C#

My question, to you, the community, is how would I call a single function to an array list of varying sizes of elements?

Say, you have the array set up and you can add array elements with gameobjects and their changeable varables, I will update with a script, once I arrive at my destination.

I'm building a simple way of adding extra scenery objects to the same function through inspector, for a mid term school project.

Any links to study and any C# scripting advice will be gladly inspected.

Comment
Add comment · Show 2
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 oswin_c · Dec 20, 2016 at 03:53 PM 0
Share

What do you mean?

Are you trying to have one function that you can call that will add a gameobject to some list?

If that's the case then all you need is to call the somelist.Add(somegameobject) function.

I'm not sure what you mean.

avatar image Glurth oswin_c · Dec 20, 2016 at 03:57 PM 0
Share

To clarify, I think oswin_c is suggesting that you use a List , and NOT an array.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by oswin_c · Dec 20, 2016 at 04:23 PM

Okay, after re-reading this, I think I know what you are asking.

You may be used to C++, where changing the size of an array involves copying the elements over to a new one. By the way, that is a perfectly valid way of doing that in C#. You absolutely can use arrays and just make a new array of the size of the old array (simply someArray.Length in C#) plus the one you want to append, then loop through and change the values.

Or.... you could use lists. A list in C# is a template array that can be any type, of arbitrary size, and allows things like searching through. They are accessed and work like an array, but easier. This is what the solution looks like in lists:

 List<GameObject> YourObjects = new List<GameObject>();
 
 void Start()
 {
 ...stuff
     YourObjects.Add(SomeInitialObject);
 }
 
 ...
 
 void Update_Or_Whatever()
 {
     YourObjects.Add(Object_That_Exists); 
     YourObjects.Add(GameObject.Instantiate(Resources.Load("object_that_doesnt_exist_yet"), wherever.transform.position, however.transform.rotation, whoever.transform));
     GameObject SomeObject = YourObjects[666];
 }

If speed is a chief concern, but you don't want to fuss with copying arrays, then consider a dictionary. They're simple to use, don't worry.

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 Owen-Reynolds · Dec 20, 2016 at 05:02 PM 0
Share

Just so you know, changing a real C# array also requires you to copy into the new one. And C++ has helpful size-changing arrays and linked-lists the same as C# does. Vector (in C++) is what List in C# is based-on.

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

7 People are following this question.

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

Related Questions

Microsoft visual studio problem 0 Answers

Increasing player speed over time? 0 Answers

How can I fix this? 0 Answers

Binding animator parameters 0 Answers

After exit from game, Unity call awake(), that is awake() calls twice in game: when game start and when player exit from game. What I should doing to avoid this? 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