Seem like because its normally a thing you do just once but now the program is creating the variable every frame is this now crazy inefficient and only ok cus the game is simple?
Hi Khari,
Could you please share an example? Creating and destroying variables in Update should not cause any issues, especially not if we don’t create millions over and over again.
From:
void Update()
{
float xValue = Input.GetAxis("Horizontal");
float zValue = Input.GetAxis("Vertical");
transform.Translate(xValue, 0, zValue);
}
My understanding is: it’s not inefficient. A float
is only 4 bytes. Creating 8 bytes locally to Update()
each iteration should not cause performance problems unless your computer is a potato.
But let’s pretend he was creating a more expensive object, one that would cause a performance problem. In that case, C# has an excellent optimiser that would uplift any expensive local initializations. (read: this thread @ stackexchange, which I think is similar to your question).
And finally, just to throw mud on the subject, references to unity components or objects are usually cached @ Class scope and assigned @ Awake()
or Start()
because searching for them is an expensive operation that I’m not certain C# knows how to optimize.
So I guess my final answer is: if it’s a problem, do whatever is sensible.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.