- Home /
Why is my list not filling
I'm trying to work on a getting my AI to Follow procedurally generated waypoints. When I try to do this however my list is empty despite using the same syntax I had used for other list storage which worked just fine. here are examples from my code
public class GameData : MonoBehaviour {
public List<GameObject> players;
public List<GameObject> waypoints
}
public class InputController : MonoBehaviour {
private void Awake()
{
//works
Singleton.gm.GetComponent<GameData>().players.Add(gameObject);
}
public class Register_Waypoints : MonoBehaviour {
// Use this for initialization
void Awake () {
//does not work
Singleton.gm.GetComponent<GameData>().waypoints.Add(gameObject);
}
}
I have essentially the same thing scripted for the possible spawn points for both my player and my AI as well as the AI itself registers inside a list in the game data script and those all work just fine but for some reason the waypoints don't show up. Why is this happening.
what happens when you execute Singleton.gm.GetComponent().waypoints.Add(gameObject);
any errors?
I'm just guessing here, because you haven't posted the code where you set Singleton.gm
.
It seems like you are setting up Singleton.gm
to provide an instance to a GameObject. Where in your code do you do that?
If you do it inside a script's Awake ()
, and it happens that this particular Awake () runs after the Awake() of Register_Waypoint
, then the instance won't be available yet.
So, if this is the case, add the gameObject to your list inside Start()
, or set the execution order of the script containing "instance setting" Awake(), so that it runs before your other scripts.
fafase: Yes I have made sure that the Register_Waypoints script is attached to the designated waypoints
shadyproductions: I do not get any errors in the console when I start up the game
pako: The singleton instance is a game manager that is a static object with the don't destroy on load function. The initial creation of this object is on a start menu screen and carries over to the actual game scene so that shouldn't be the issue. I have also went in to the script run order and specifically set the script to run after the level is built and so there shouldn't be any issues with the order in which it runs. at least there shouldn't be to my knowledge.
How do you initialize the 2 Lists? Do you set their length in the Inspector or something? Of course, if there were no errors in the console, they have to be initialized somewhere...
As far as the lists themselves go what you see in game data is all there is. Like I said I have several other lists that I create in the same manner but they all seem to work just as the players one shown does. The way I have it set up to the best of my understanding explanation is that the game data script defines the list then that list is more or less initialized either at the point it is defined or when an item is added to that list. This is probably not an accurate explanation of what is happening but it is my best guess based on what seems to be happening and working.
$$anonymous$$aybe on some other part you are removing elements of the list, or maybe there's a problem with the Singleton.gm.
What happens if you add the waypoints to the players list?
Answer by dracolytch · Dec 18, 2017 at 04:40 PM
I think your problem is that you're doing this in Awake. The initialization order of things isn't deterministic or terribly predictable, which is why there are two initialization phases: Awake and Start.
When you do something in Awake, other GameObjects and Components may not be defined yet, causing a host of issues and unpredictable behavior. Use Awake for internal initialization only.
Start gets called after all active GameObjects have gone through their Awake. While the order in which the different starts are called is not defined, you know that everything has at least gone through Awake. This means that external references should work/be placed in Start.
Keep in mind that during Start, other items may not have gone through Start yet, so their state might not be quite what you expect.
Awake for internal initialization. Start for cross-object initialization.
That's what I thought at first too but in reverse. Initially it was in the start function however I noticed that in my other scripts that it worked it was in the awake function so I thought I would try to put it there to see if it would work. Unfortunately, as this question exits on here it did not.