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 /
  • Help Room /
avatar image
0
Question by Jeroencb · Jan 20, 2018 at 03:58 PM · instantiatelist

Instantiate working in Awake but on in Update

So I have a list of gameobjects, the list is linked to some UI elements through which I can click to decide which gameobject I want to create. To try out this code I wrote a simple timer in my update to create the object selected in my UI elemet every 3 seconds.

activeBuilding is the var that the list adjusts to decide which building to instantiate

When I call the instantiate line in Awake it works fine (with the list number assigned directly in the code to activeBuilding). However, when I try the same thing in Update it gives me an "Argumentoutofrangeexpection: argument is out of range".

Im not sure what I am doing wrong here, please let me know if you need any more information to help me out! This is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Buildings : MonoBehaviour {
 
     private int BuildingType;
     private int BuildingGroup;
 
     public List<GameObject> BuildingList;
 
     private int activeBuilding = 3;
 
     private float time = 0;
 
     public void selectBuilding(int index)
     {
         activeBuilding = index;     
     }
     private void Awake()
     {
         selectBuilding(0);
     }
     public void BuildingCreation()
     {
     }
     private void Start () {
         BuildingList = new List<GameObject>();
     }
     public void Update () {
 
         selectBuilding(activeBuilding);
 
         time += Time.deltaTime;
 
         if (time > 3)
         {
             Debug.Log("active building = " + activeBuilding);
             Instantiate(BuildingList[activeBuilding], new Vector3(10, 01, 10), Quaternion.identity);
             time = 0;
         }
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kishotta · Jan 20, 2018 at 04:13 PM

Are you assigning BuildingList in the inspector? If so, when you initialize it in Start(), you are nuking the list and replacing it with a new 0-length list (so any index will be out of range). You can initialize the list on declaration instead:

 public List<GameObject> BuildingList = new List<GameObject> ();
Comment
Add comment · Show 3 · 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 Jeroencb · Jan 20, 2018 at 04:18 PM 0
Share

Yeah that did the trick, thanks!

I dont fully understand why the Start() would empty my list though?

avatar image Kishotta Jeroencb · Jan 20, 2018 at 04:23 PM 1
Share

Start gets called after Awake, but only on the first frame. So in Awake you are setting activeBuilding to 0, then in Start you are setting BuildingList to a brand new empty list.

I wish I could tell you the specifics (and someone around here probably can) but the declaration initialization essentially happens when the object is created (before any $$anonymous$$onoBehaviour stuff happens), but is overridden by Inspector values.

This image may help you understand the issue a little better, too.

avatar image Jeroencb Kishotta · Jan 20, 2018 at 04:31 PM 0
Share

Thank you for the explanation that clears it up a bit! And thank you for your time :)

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

94 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

Related Questions

Destroy GameObject in list then instantiate GameObject not working 1 Answer

Prevent spawning if the gameobject has same position as a list of Vector3s using another list? 1 Answer

Add Clones of GameObject to List(array) 0 Answers

Instantiating gameObjects, adding them to a list then moving every gameObject in the list.,Moving gameobjects in gameobject list 0 Answers

C# Add Unique Listener to Buttons Instantiated from List 0 Answers


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