Script Execution Order is the same

So basically I have this script that would randomize the location of the GameObject every “spawnTime” and it is doing its job.

However, I thought the code meant that its transform.position would be adjusted again after it is within a distance of 4 from the gameObject “Player” However it seem that even though both this script and the script containing “OnTriggerEnter” on my “Player” gameObject were executed at the default time (not utilizing “Script Execution Order”,

the transform.position of this gameObject would be adjusted before taking into account the “OnTriggerEnter” of the “player” gameObject

I would like for someone to confirm what I wrote in bold above is true or explain why it is wrong. I thought that it would’ve run the command for “OnTriggerEnter” and changing the transform.position of this gameobjec at the same time.

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Rotator : MonoBehaviour {

    public float spawnTime;
    public float startingSpawnTime;
    public float distance;
    public GameObject player;

    private void Start()
    {
        distance = 4;
        InvokeRepeating("Spawn", startingSpawnTime, spawnTime);
    }

    void Update ()
    {
        transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
    }

    void Spawn()
    {
        do
        {
            transform.position = new Vector3(Random.Range(-9, 9), 0.5f, Random.Range(-9, 9));
        } while ((player.transform.position - transform.position).magnitude < distance);
    }
}

@haidao0923, Based on this documentation,

I’d have to nail down exactly what you mean before I could say.

If you are asking if the assigned position from the repeated calls to Spawn are synchronized with the OnTriggerXXX function calls, then no. I see nothing that documents how the execution order of an InvokeRepeating call will mesh in timing with, well, anything. As far as I can see, the documentation is silent, and therefore any behavioral observations we make for any given version of Unity could change.

If, on the other hand, you’re asking if the rotation in Update (which you may be excluding by specifically saying transform.position, I realize), then I can say no because the docs state the OnTriggerXXX calls are performed before Update (but after FixedUpdate) calls.

Indeed, the OnTriggerXXX functions are associated with the fixed update loop.