- Home /
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);
}
}
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;
"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?
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
Follow this Question
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