So there’s a very simple concept in C# that I haven’t really gotten to wrapping my head around…
Say I create a list like this:
public class MyClassFirst {
public List<string> listOfStrings = new List<string>();
}
I’m not understanding how I would reference the list from a second …ummm…‘non-value’ (like a pointer kind of) in a second class without creating any new list object in MySecondSecond here:
public class MyClassSecond : MonoBehaviour {
declare listOfStringsReference without creating a new list here
MyClassFirst myClassFirst;
Start() {
myClassFirst = new MyClassFirst(); //now we're creating a new MyClassFirst?
listOfStringsReference = myClassFirst.listOfStrings;
}
}
I’ve been trying to understand the ref keyword. Is that how you do it? So would it be like:
ref List<string> MyClassFirst.listOfStrings myClassFirstsListOfStrings
Or something…I don’t know…
There’s just something in me which feels like it wouldn’t be the best to just create new lists every frame for a script that’s going to be attached to like 1000 game objects in my game…It even feels wrong above to use the new keyword so many times…Is it really okay performance wise to just do that 10,000 times in a frame, even if myClassFirst has all these other empty lists that were being created in that frame? If game objects held these scripts using the new keyword, wouldn’t that mean hundreds of thousands of objects created with the new keyword just on a single frame?
Obviously there’s something very simple I’m not understanding here.
My most basic question is to clarify: How do I reference the list without creating a new one, so I can change it’s values without creating a new list object?