- Home /
Method in Start() not running, but in Update(), why?
Hey, im currently at building an automatic save and load system. On the Editor in Unity its all working well. But for some reason if i start the Game on my Phone, the loading of my inventory works, but the Player position does not. Even if i do the exact same thing with the data, and how i save and load it.
For test purposes i run the "LoadPlayer()" method in Update once, and like this it is working. It really confuses me that it is working like this:
void FixedUpdate()
{
if(!loadedPos){
LoadPlayer();
loadedPos = true;
}
}
but not like this:
void Start(){
LoadPlayer();
...
}
Does anyone have an idea what could cause that?
my inventory is loading in the second way btw.
Thanks for every help!
I'd be very surprised if it was "not running", I think it's much more likely that it's running too early to function as you'd expect. What's happening in LoadPlayer and what effects are you seeing that make you think it's not running?
Answer by UnityM0nk3y · Apr 07, 2021 at 08:39 AM
To my understanding:
The reason why "FixedUpdate" worked, and not "Start" is because "FixedUpdate" are called for every "Physics Step". Meaning it happens after "Start".
The problem is probably with "LoadPlayer();", this method has to be run after another method, and "Start" is too early.
To test this theory try this:
private void Awake()
{
StartCoroutine(loadPlayerDelayed());
}
IEnumerator loadPlayerDelayed()
{
yield return new WaitForSeconds(0.1f);
LoadPlayer();
Debug.Log("PlayerLoaded");
}
Ahh ye that sounds logical! with your method it worked out aswell! the Loadplayer method looks like this (just realized that might be an intresting information):
public void LoadPlayer(){
PlayerData data = SaveSystem.LoadPlayer();
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
transform.position = position;
}
I had similar issues when I used to develop for Andriod. The problem is that the phone can only handle so much "processing" at the start of your game, so perhaps do use a "delayed Load" routine, and just close your loading screen afterwards?
Answer by singhlaxman9761 · Apr 08, 2021 at 12:58 PM
If is there any exception occur in Awake() function then Start() function will not call. Make sure there is not any exception in Awake() function.:)
The original post isn't using Awake()
at all. Please read the questions.
Your answer
Follow this Question
Related Questions
how to properly save an int value ? 2 Answers
What Happens in Scene Load 1 Answer
failed to start unity package manager operation timed out 9 Answers
Start menu gui wont load scene 2 Answers