Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Camman012 · Dec 18, 2017 at 10:28 AM · unity 5scripting problemlists

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Dec 18, 2017 at 11:39 AM 0
Share

You sure the Register_Waypoints is active?

avatar image ShadyProductions · Dec 18, 2017 at 02:17 PM 0
Share

what happens when you execute Singleton.gm.GetComponent().waypoints.Add(gameObject);

any errors?

avatar image pako · Dec 18, 2017 at 02:45 PM 1
Share

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.

avatar image Camman012 · Dec 19, 2017 at 12:00 AM 0
Share

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.

avatar image pako Camman012 · Dec 19, 2017 at 10:05 AM 0
Share

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...

avatar image Camman012 pako · Dec 20, 2017 at 12:24 AM 0
Share

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.

Show more comments
avatar image fdz_ · Dec 20, 2017 at 12:48 AM 0
Share

$$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?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Camman012 · Dec 18, 2017 at 10:40 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Characterdoesn't run on Aiming mode 0 Answers

Sprite dots spawn and have their own color, but they only show as wireframes? 0 Answers

Unity 5.3.3f1: How to set an object as AssetBundle by script? 0 Answers

[Shooting shots Scripting] How to make the compiler know that I mean the empty quad by the Public Transform? 0 Answers

How to Set Animation Position on Slope (animation) help please 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges