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.
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.
To clarify, I think oswin_c is suggesting that you use a List , and NOT an array.
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.
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
Follow this Question
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