A GameObject function in Start() doesn't let the next actions to be executed after it
Hi! I have a GameObject function in a Start() with the purpose of finding an object with a specific tag which is closest to the object which the script is referenced to. This is the Start() code from the script. Everything before "FindClosestIntersectionTarget" method is correctly executed; everything after it is like they don't exist :
private void Start()
{
Player1 = GameObject.FindGameObjectWithTag("Player1");
Player2 = GameObject.FindGameObjectWithTag("Player2");
Player3 = GameObject.FindGameObjectWithTag("Player3");
Player4 = GameObject.FindGameObjectWithTag("Player4");
Arrow = GetComponent<Animator>();
FindClosestIntersectionTarget(currentPathArrowManagerEgypt);
pathArrowScript = currentPathArrowManagerEgypt.GetComponent<PathArrowManagerEgypt>();
gameControllerObj = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();
switch (pathArrowScript.playerinIntersection)
{
case 1:
SelectedPlayer = Player1;
break;
case 2:
SelectedPlayer = Player2;
break;
case 3:
SelectedPlayer = Player3;
break;
case 4:
SelectedPlayer = Player4;
break;
}
}
This is the code inside "FindClosestIntersectionTarget()" method:
GameObject FindClosestIntersectionTarget(GameObject currentPathArrowManagerEgypt)
{
Vector3 arrowPosition = transform.position;
return GameObject.FindGameObjectsWithTag("PathArrowEgypt1")
.OrderBy(o => (o.transform.position - arrowPosition).sqrMagnitude)
.FirstOrDefault();
}
As you can see, nothing special. There is something about GameObject functions which I'm overlooking?
Answer by tormentoarmagedoom · Mar 11, 2019 at 11:31 AM
Good day.
When a script gives an error like nullreference or something else, it normally stops running. So this can be the cause it does not execute the following lines of code. Make sure there is no error, or put some if - else to prevent code stops if does not get some return or something.
Bye!
Your answer
Follow this Question
Related Questions
Start() not called immediately in build, working fine in editor 0 Answers
How to avoid game starting before start button is clicked? 1 Answer
win game & loos game 0 Answers
How can I put buttons on a Cube? 0 Answers
Disable mouse click on gameObject 1 Answer