Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 mauriciomb · Jun 25, 2018 at 02:32 AM · referencingfor loopprefab connection

Unity Freezing with for loop and prefab referencing

My main objective is to spawn some prefabs objects every couple of seconds from another prefab and have the spawned prefab travel to a set destination.

I am using invokeRepeating to spawn them every couple of seconds. I am using a For Loop to go through each element in a list and test whether or not they are greater than or equal to 50.

if condition is true then the for loop will instantiate the prefab and will also pass the i value to a variable in the newly spawned prefab script I am instantiating (i = factionNumber;). the variable listNumber in the spawned prefab script is an int that I am using to represent an element. the problem is that it completely freezes Unity. Any help would be appreciated. I am somewhat new to using loops so if there is a better way to accomplish this please let me know. Thanks in advance!

P.S. i = tradeShipScript.listNumber; is causing the freezing.

 public class TradingAndTradeShips : MonoBehaviour {
     public List<float> factionRelationship = new List<float>();
     public NeutralAIMovement tradeShipScript;
     public GameObject tradeShip;
     public float waitToSpawn;
     public int factionNumber;
 
     void Start ()
     {
         factionNumber = 0;
         InvokeRepeating("spawnTradeShip", 0, waitToSpawn);
     }
 
     void spawnTradeShip()
     {
         for (int i = 0; i < factionRelationship.Count; i++)
         {
             if (factionRelationship[i] >= 50f)
             {
                 GameObject tradeObj = Instantiate(tradeShip, new Vector3(transform.position.x, transform.position.y, tradeShip.transform.position.z), tradeShip.transform.rotation);
                 i = factionNumber;
             }
         }
     }
 
 public class NeutralAIMovement : MonoBehaviour {
     public List<Transform> tradeEndPointList = new List<Transform>();
     public PlayerMovement player;
     public TradingAndTradeShips homeRelationship;
     public int listNumber;
     public float speed;
     void Start () {
         listNumber = homeRelationship.factionNumber;        
     }
     
     void Update ()
     {
         transform.position = Vector2.MoveTowards(transform.position, tradeEndPointList[listNumber].position, speed * Time.fixedDeltaTime);
     }
 }

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
0

Answer by hectorux · Jun 25, 2018 at 03:54 AM

First of all you are using some things bad. Time.fixedDeltaTime isn't well use if you are in the Update, it is use better in the fixedUpdate because it will always be 0.02f wich will be your fixed update ratio. Also instead of using Ivoke repeating, you should use a Coroutine:

 void Start(){
 
 StartCoroutine(SpawnTradeShip);
 
 }

 IEnumerator SpawnTradeShip()
      {
 while(iWantToLoop){
          for (int i = 0; i < factionRelationship.Count; i++)
          {
              if (factionRelationship[i] >= 50f)
              {
                  GameObject tradeObj = Instantiate(tradeShip, new Vector3(transform.position.x, transform.position.y, tradeShip.transform.position.z), tradeShip.transform.rotation);
                  i = factionNumber;
              }
 yield return new WaitForSeconds(waitToSpawn);
 //i assume waitToSpawn is th time between loops
              }
          }
      }

For last, you are setting factionNumber = 0; and then giving that value to listNumber = homeRelationship.factionNumber; maybe you wanted i = factionNumber; to be factionNumber=i;

Comment
Add comment · Show 2 · 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 mauriciomb · Jun 25, 2018 at 04:05 AM 0
Share

"maybe you wanted i = factionNumber."

Wow I don't know what I was thinking there. You are absolutely correct.

Thanks for letting me know about fixedDeltaTime. I read somewhere that it was more accurate than DeltaTime but I didn't realize that fixedDeltaTime was better used in FixedUpdate.

If you could please tell me why I should used co-routines as opposed to using invoke?

avatar image mauriciomb · Jun 25, 2018 at 01:17 PM 0
Share

Your code works great. Thank you!

The problem I am getting now is that factionNumber is not changing from 0 when I instantiate the prefab.

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

85 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

Related Questions

Reference specific variable from a specific gameObject? 2 Answers

public script reference that can be used for lots of scripts 1 Answer

Accessing a variable from one script in another. 2 Answers

multi reference a specific game object to a script that it's attached to many game objects ? 1 Answer

Get typeof component 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