Sorry for my bad English. This error from my object, because if it dont exist in scene unity work fine. When i enter play mode everything okay, but if i dont stop play mode and go to the scene editor, after 2 seconds unity freeze, and i need from task manager end unity task.
Code from my object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Playables;
using Random = UnityEngine.Random;
public class VisitorScript : MonoBehaviour
{
[SerializeField]
List<int> productsBuy = new List<int>();
[SerializeField]
List<GameObject> shelfs = new List<GameObject>();
[SerializeField]
List<GameObject> shelfsWithNeedProducts = new List<GameObject>();
[SerializeField]
List<int> productsNeedToBuy = new List<int>();
Transform currentShelf;
List<int> productsInCart = new List<int>();
public bool isGo = false;
public bool endShopping = false;
public GameObject MainShelfs;
public GameObject productManager;
public GameObject exit;
public GameObject kashier;
private Animator animator;
private NavMeshAgent agent;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
productManager = GameObject.Find("ProductManager");
MainShelfs = GameObject.FindWithTag("mainShelfs");
kashier = GameObject.FindWithTag("Cashier");
exit = GameObject.FindWithTag("Exit");
}
public IEnumerator visitorCoroutine()
{
while (true)
{
if (shelfsWithNeedProducts.Count > 0)
{
goToShelf(shelfsWithNeedProducts[0].transform);
currentShelf = shelfsWithNeedProducts[0].transform;
}
else if (productsInCart.Count > 0) { agent.SetDestination(new Vector3(kashier.transform.position.x, kashier.transform.position.y, kashier.transform.position.z)); }
if (agent.remainingDistance > 1f) { continue; }
else {
isGo = false;
yield return new WaitForSeconds(Random.Range(5, 9));
if(currentShelf.Find("shelftrigger").GetComponent<shelfScript>().count > 0)
{
productsInCart.Add(currentShelf.Find("shelftrigger").GetComponent<shelfScript>().itemId);
shelfsWithNeedProducts.Remove(currentShelf.gameObject);
}
}
}
}
void Start()
{
for (int i = 0; i < Random.Range(2, 7); i++) { productsBuy.Add(Random.Range(0, productManager.GetComponent<ProductManager>().products.Count)); Debug.Log(productsBuy[i]); }
foreach (Transform shelf in MainShelfs.transform) { shelfs.Add(shelf.gameObject); }
for (int i = 0; i < shelfs.Count; i++)
{
if (shelfs[i].transform.Find("shelftrigger").GetComponent<shelfScript>().itemId >= 0 && shelfs[i].transform.Find("shelftrigger").GetComponent<shelfScript>().count > 0)
{
if (productsBuy.Contains(shelfs[i].transform.Find("shelftrigger").GetComponent<shelfScript>().itemId) && productsBuy[productsBuy.IndexOf(shelfs[i].transform.Find("shelftrigger").GetComponent<shelfScript>().itemId)] == shelfs[i].transform.Find("shelftrigger").GetComponent<shelfScript>().itemId)
{
shelfsWithNeedProducts.Add(shelfs[i]);
while (true)
{
if ( productsBuy.Contains(shelfs[i].transform.Find("shelftrigger").GetComponent<shelfScript>().itemId) == true )
{
productsNeedToBuy.Add(shelfs[i].transform.Find("shelftrigger").GetComponent<shelfScript>().itemId);
productsBuy.Remove(shelfs[i].transform.Find("shelftrigger").GetComponent<shelfScript>().itemId);
} else { break; }
}
}
}
}
if(shelfsWithNeedProducts.Count == 0)
{
agent.SetDestination(new Vector3(exit.transform.position.x, exit.transform.position.y, exit.transform.position.z));
}
else { StartCoroutine(visitorCoroutine()); }
}
void Update()
{
animator.SetFloat("Speed", agent.speed);
}
void goToShelf(Transform shelfTransform)
{
agent.SetDestination(new Vector3(shelfTransform.position.x, shelfTransform.position.y, shelfTransform.position.z));
currentShelf = shelfTransform;
isGo = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Exit") { Destroy(gameObject); }
}
}