Skip to main content

Calling methods from a separate script

Let's say you have script A that accesses a method from script B.

Would it be more performant to merge script A and B together? Let's see!


public Fibonacci FibonacciInstance;

[RecursiveMethod]
private int FibonacciRecursive(int n)
{
	if (n <= 0)
		return 0;
	else
		return 1 + FibonacciRecursive(n - 1);
}

public override void Benchmark1()
{
	for (int i = 0; i < 10000; i++)
	{
		FibonacciRecursive(2);
	}
}

public override void Benchmark2()
{
	for (int i = 0; i < 10000; i++)
	{
		FibonacciInstance.FibonacciRecursive(2);
	}
}

Both methods do the same thing :

  • Benchmark1 calls FibonacciRecursive in the same script
  • Benchmark2 calls FibonacciRecursive from a separate script

B1 : 236.120 ms 
B2 : 305.513ms (1.29 times slower)

So yes, calling a method from a separate script affects the performance.