Я пытаюсь составить список для хранения всех игровых объектов.
Что бы я ни пытался, Unity сообщает мне, что я не могу использовать GetName, кроме как в основном потоке (я понятия не имею, куда я вызываю GetName, поскольку я только объявляю список). Ошибка:
UnityException: GetName можно вызвать только из основного потока.
Вот трассировка стека:
UnityException: GetName can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Object.GetName () (at \:0)
UnityEngine.Object.get_name () (at \:0)
UnityEngine.TextCore.Text.FontAsset.ReadFontAssetDefinition () (at \:0)
UnityEngine.TextCore.Text.FontAsset.GetCharacterInLookupCache (System.UInt32 unicode, UnityEngine.TextCore.Text.FontStyles fontStyle, UnityEngine.TextCore.Text.TextFontWeight fontWeight, UnityEngine.TextCore.Text.Character& character) (at \:0)
UnityEngine.TextCore.Text.FontAssetUtilities.GetCharacterFromFontAsset_Internal (System.UInt32 unicode, UnityEngine.TextCore.Text.FontAsset sourceFontAsset, System.Boolean includeFallbacks, UnityEngine.TextCore.Text.FontStyles fontStyle, UnityEngine.TextCore.Text.TextFontWeight fontWeight, System.Boolean& isAlternativeTypeface, System.Boolean populateLigatures) (at \:0)
UnityEngine.TextCore.Text.FontAssetUtilities.GetCharacterFromFontAssetsInternal (System.UInt32 unicode, System.Collections.Generic.List\`1\[T\] fontAssets, System.Boolean includeFallbacks, UnityEngine.TextCore.Text.FontStyles fontStyle, UnityEngine.TextCore.Text.TextFontWeight fontWeight, System.Boolean& isAlternativeTypeface, System.Boolean populateLigatures) (at \:0)
UnityEngine.TextCore.Text.FontAssetUtilities.GetCharacterFromFontAssetsInternal (System.UInt32 unicode, UnityEngine.TextCore.Text.FontAsset sourceFontAsset, System.Collections.Generic.List\`1\[T\] fontAssets, System.Collections.Generic.List\`1\[T\] OSFallbackList, System.Boolean includeFallbacks, UnityEngine.TextCore.Text.FontStyles fontStyle, UnityEngine.TextCore.Text.TextFontWeight fontWeight, System.Boolean& isAlternativeTypeface, System.Boolean populateLigatures) (at \:0)
UnityEngine.TextCore.Text.TextGenerator.GetEllipsisSpecialCharacter (UnityEngine.TextCore.Text.TextGenerationSettings generationSettings) (at \:0)
UnityEngine.TextCore.Text.TextGenerator.GetSpecialCharacters (UnityEngine.TextCore.Text.TextGenerationSettings generationSettings) (at \:0)
UnityEngine.TextCore.Text.TextGenerator.PrepareFontAsset (UnityEngine.TextCore.Text.TextGenerationSettings generationSettings) (at \:0)
UnityEngine.TextCore.Text.TextHandle.PrepareFontAsset () (at \:0)
UnityEngine.UIElements.UITKTextJobSystem+PrepareTextJobData.Execute (System.Int32 index) (at \:0)
Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct\`1\[T\].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at \:0)
Вот мой код (строки, закомментированные в IEnumerator, закомментированы, чтобы я мог попытаться выполнить отладку):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public List targets;
private float spawnRate = 1;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
StartCoroutine(SpawnTarget());
}
IEnumerator SpawnTarget()
{
while(true)
{
yield return new WaitForSeconds(spawnRate);
// int index = Random.Range(0, targets.Count);
// Instantiate(targets[index]);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... -exception