The question is answered, right answer was accepted
Strange code execution
Hello guys!
I encountered a strange behaviour in my code execution. The idea is the following: I am calling a method which will instantiate an object and add it to a list. The problem is that the code line after the method call gets executed before the called method finishes the execution so for example, if my list count is 0, I call the method to insert an element and right after the call I log the list size... it will display 0 even though the count of the list will change its value to 1 in a future frame.
someMethod()
{
...(code)
otherClassRef.InstatiationMethod()
Debug.Log("Call 2");
}
OtherClass
InstantiationMethod()
{
...(call to instantiate prefab and insertion in the list)
Debug.Log("Call 1");
}
//The code will display:
Call 2
Call 1
I am not using any coroutines so since the code is executed sequentially the right log display should be Call 1, Call 2. Am I missing something?
Answer by karma0413 · Nov 27, 2017 at 09:17 AM
EDITED: for clarity
If you run a bit of code on CLASS A and then call a function on a completely separate script. That sub-routine on CLASS B will run before CLASS A finishes the entire code.
As such you should be seeing Call 1 prior to Call2
Yes you understood the idea behind my question. Thank you for your answer but I am not sure what to say about your answer regarding the execution. Shouldn't the execution of the line after the method call be 'blocked' until the method returns from execution? (of course asu$$anonymous$$g that the method doesn't start a new thread, which is not the case in my situation) Or this is applied only when the method is part of the same class? I have to admit that I am a bit confused right now...
No, and I updated my original response to accurately reflect....
When you call a function on another different script.... that specific function will run before the code in CLASS A finishes.
I went just now and opened up a new project and ran a test to confirm this.
linkto2 is just a reference to the object where the 2nd script is located.... JUST FYI
public GameObject linkTo2;
public script2 _scripty;
// Use this for initialization
void Start () {
_scripty = linkTo2.GetComponent<script2> ();
}
// Update is called once per frame
void Update () {
print ("Hello");
_scripty.SendCommand ();
print ("This text should come AFTER the sub-routine");
}
}
Blockquote
void Start () {
}
// Update is called once per frame
void Update () {
}
public void SendCommand(){
print ("I received the command");
}
well yea, that was exactly my point. And the problem is that in my case it doesn't execute that way...
You're right (this answer looks like nonsense to me - note this was written before the answer was changed to say the opposite of what it originally did). If there's no threading or coroutines involved, then (given your pseudo-code) the call to Instantiation$$anonymous$$ethod will "run immediately" and should run to completion before the "Call 2" debug line is called. I'd be reluctant to say more without seeing the actual code/output, though.
Thank you for your answer! After a more detailed display of Log messages and debugging using the Visual Studio Debugger I think I managed to find the issue... In the instantiation method I am making the instantiation based on a check of null on an object (which I thought it would be null at the moment I am interested in - but actually it isn't - because it was destroyed in the same frame so its reference will become null only in the next frame if I am not wrong) - so my method won't make any actual instantiation at that moment... The moment which I am talking about is a special case which I have to treat separately. The object I check for null in my instantiation method will actually become null only in the next frame resulting in an instantiation and an increment in my list. Also a thing that I must point out is the fact that I was displaying the Log only on that specific null case... and didn't realized that it was entering in that branch only in the next frame.
Follow this Question
Related Questions
Store instantiated object in List 2 Answers
Only spawning power ups that the player wants in that game 1 Answer
C# How can create a list of floats from different types of structs? 0 Answers
Declared variable being returned as null in a method? 0 Answers
How to call a method only once after some time has elapsed? 1 Answer