I was wondering if anyone could shed some light on the following:
class foobar
{
var value:int;
function foobar()
{
value++;
print("Hi");
Debug.Log("Hello: " + value);
}
}
When I instatiate foobar, I do not receive any messages from print, and Debug.Log is called 3 times. However value is only incremented once. Can someone give me an overview of what is going on under the hood here?
I have noticed that sometimes certain things are called 3 times as well. In my latest projects though I followed a very strict set of rules about when Update, LateUpdate etc. are used to initialise scripts and found that it seemed to solve this issue.
So I’d love to know a detailed answer to this.
But it would probably help to show what is starting this script.
I don’t think it’s actually calling the constructor multiple times, it’s more that some of the debug function calls made within it are being executed more than once.
You’ll also notice (if you have the same setup) that the Debug.Log calls are called again, twice, when the game is shutdown. Which is ridiculous.
I have noticed that does happen sometimes.
My current project does not do this though.
However in my current project if I call Debug.Log in a loop, say Debug.Log(i) in a for loop it only works if the value changes, if the value does not change it will not repeat it. The only way I got it to repeat the value when it was meant to is by placing everything in Update instead of using the other called function names.
I’d love if one of the unity devs could tell us what is going on here.