- Home /
How to return control back to the original script?
In a script, I have to access a public function in another script to execute it's functionality. I usually do this via someGameObject.GetComponent<SomeComponent>().publicFunction()
. But the problem that occurs is the control won't come back to the original script after the function execution is over. Suppose the above statement is between some lines of code like -
statement 1
statement 2
statement 3
someGameObject.GetComponent<SomeComponent>().publicFunction()
statement 4
statement 5
statement 6
The statements from 4 to the end of the current function from which it's being called are getting skipped, or ignored because the execution control never returns back from the other script to execute the remaining statements. How can I solve this? Thanks in advance.
What do you mean by "return control"? What is "control" here and what is keeping you from calling it anywhere?
By control I meant control flow. And by return control I meant that the flow of instruction execution should return back to the original script from where I called the function to the another script. In my mentioned example, the flow should go from statement 1 to 3, then execute the function in another script, then execute statements 4 to 6.
That should be fine then. Are there any issues in the console? There's no reason for the block being executed (that makes the call to your other component's function) to do anything but continue, unless an issue was encountered in the function.
Are you sure that the component isn't null?
Sounds strange. A function only ends if: 1) It reaches the end 2) It reaches a return statement 3) An exception is thrown (i.e. an error occurs) One of those three things must be happening. Do you get an error message? Can you post some of the code?
The script calling the function :-
public void SwitchWallpoint()
{
_endPoint.SetActive(true);
_currentControlledWallObject = _endPoint;
InitialWallPieceSpawn();
_currentControlledWallObject.GetComponent<BuildingSystems>().ActivateGhost();
}
void InitialWallPieceSpawn()
{
GameObject wallPiece = Instantiate(_wallPiecePrefab) as GameObject;
wallPiece.SetActive(true);
wallPiece.transform.SetParent(_endPoint.transform.GetChild(2).gameObject.transform);
wallPiece.transform.position = _endPoint.transform.GetChild(2).gameObject.transform.position + new Vector3(0,0,17);
wallPiece.GetComponent<WallPieceDoublyLinkedList>().wallIndex = 0;
}
The function that is being called in another script:-
public void ActivateGhost()
{
_ghostRedRender.SetActive(true);
_ghostGreenRender.SetActive(false);
foreach (GameObject gObject in _building$$anonymous$$esh)
{
gObject.SetActive(false);
}
_foundation$$anonymous$$esh.SetActive(false);
_unitPositionPlaceHoldersReference.SetActive(false);
gameObject.GetComponent<NavmeshCut>().enabled = false;
gameObject.transform.Find("SelectionBoxHalo").gameObject.SetActive(true);
}
just wondering does it execute all those bools? have you tried to make a debug.log somtehing at the end of the excutional part? as well after the line where its called in your first script?
Answer by tormentoarmagedoom · Sep 14, 2018 at 10:13 PM
Good day.
"the execution control never returns back from the other script"
how? this is not posible, if there is not any "return;", so when the publicFunction() ends, (is not infinite) next will be statement 4.
I can only imagine..:.
1- You are getting an error in that publicFunction and you ahve deactivated the errors alerts
2- The public function is disabling the script or the object containing the script.
3- You have some infinite loop (for example a bod designed "while") in tat publicFunction
I can not imagine other possibilities...
Good day to you. I double checked again, including all the errors. Nothing turned up as per your mentioned points. 1. No error related to that function is turning up in the game. 2. The script isn't getting disabled. 3. No while loop in the public function.
This is the public function code -
public void ActivateGhost()
{
_ghostRedRender.SetActive(true);
_ghostGreenRender.SetActive(false);
foreach (GameObject gObject in _building$$anonymous$$esh)
{
gObject.SetActive(false);
}
_foundation$$anonymous$$esh.SetActive(false);
_unitPositionPlaceHoldersReference.SetActive(false);
gameObject.GetComponent<NavmeshCut>().enabled = false;
gameObject.transform.Find("SelectionBoxHalo").gameObject.SetActive(true);
}
Now I got a temporary fix. I optimized the function a bit, by removing the "Find" operation with a public gameObject reference method. So this
gameObject.transform.Find("SelectionBoxHalo").gameObject.SetActive(true)
now becomes this
_selectionBoxHalo.SetActive(true)
where _selectionBoxHalo is a public gameObject which is manually assigned in the inspector by me. So there's no need for the gameObject to be searched when it is now made as a reference. Interestingly, when I code the statements as such
public void SwitchWallpoint()
{
_endPoint.SetActive(true);
_currentControlledWallObject = _endPoint;
// Statements under consideration
InitialWallPieceSpawn();
_currentControlledWallObject.GetComponent<BuildingSystems>().ActivateGhost();
// -------------------//
}
void InitialWallPieceSpawn()
{
GameObject wallPiece = Instantiate(_wallPiecePrefab) as GameObject;
wallPiece.SetActive(true);
wallPiece.transform.SetParent(_endPoint.transform.GetChild(2).gameObject.transform);
wallPiece.transform.position = _endPoint.transform.GetChild(2).gameObject.transform.position + new Vector3(0,0,17);
wallPiece.GetComponent<WallPieceDoublyLinkedList>().wallIndex = 0;
}
the ActivateGhost() function doen't get executed, even though the wallPiece.GetComponent<WallPieceDoublyLinkedList>().wallIndex = 0
time-complexity is less. When both the statements are written like this,
_currentControlledWallObject.GetComponent<BuildingSystems>().ActivateGhost();
InitialWallPieceSpawn();
the statements work fine. This can be a bit of a problem because even with this much of optimizing, the Unity compiler straight-away gives up execution of statements when cross-scripting reference is used. And the project is in early development stages, so a lot of functionality may be added to it in the succeeding stages, which may again give rise to issues.