- Home /
Trying to create a generic functions for instantiations
Hello,
I've been trying to create a custom function for instantiating modules/scriptable objects.
The idea was that I'd input the "prefab" scriptable object and the object that was supposed to hold the instance of the scriptableObject.
public static void CreateComponentInstance<T>(T _Type, T _instance) where T : Object
{
if (_Type == null) Debug.Log("No instance of _Type entered.");
else _instance = Object.Instantiate(_Type);
}
I would then execute this function like this:
Util.CreateComponentInstance(mainDriveType,mainDrive);
The problem is after executing this, "mainDrive" is still null. And I don't really understand why. If I put an object into a function, I'm just transferring a reference, right? So if I change something with the reference, I should also change the original object? _instance isn't null after the instantiation, but "mainDrive" is.
What did I get wrong here?
Answer by Bunny83 · Dec 30, 2020 at 03:44 PM
Method parameters are always passed by value (== copied) unless you have a "ref" or "out" parameter. For more information see my answer over here.
Something like this would work:
public static void CreateComponentInstance<T>(T aPrefab, ref T aInstance) where T : Object
{
if (aPrefab == null)
Debug.Log("No prefab provided.");
else
aInstance = Object.Instantiate(aPrefab);
}
I don't quite see the point of your method though. Your method name "CreateComponentInstance" also doesn't make much sense because that's not what that method does- All you do is creating a new object if the provided prefab / source object is not null. The newly created object is stored in the variable that is passed in by reference. So it's just the same as Instantiate but does log a message if no source object is provided-
Thats not true. Reference types passed into methods are passed by reference (albeit a copy of the reference).
The code below demonstrates that and outputs 0 then 12
public class TestClass
{
public int Number { get; set; }
}
class Program
{
public static void ChangeNumber(TestClass testClass)
{
testClass.Number = 12;
}
static void $$anonymous$$ain(string[] args)
{
var test = new TestClass();
Console.WriteLine(test.Number);
ChangeNumber(test);
Console.WriteLine(test.Number);
}
}
That's exactly the thing which seems to confuse most people. A lot seem to confuse the concept of a reference and the concept of a variable as well. A reference is simply put just an address. A variable is a memory location that can store a value (no matter if it's a value-type or reference type value).
A ref parameter is a direct pointer to the "variable". So changes to that parameter will affect the actual variable. Parameters that are not ref or out parameters are local variables to the method. They live in the stack frame of the method when the method is called.
Been working in c++ for too long I keep forgetting passing reference types into methods (without specifying ref) in c# is not analogous to just passing a reference in.