Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
class Enemy
{
public string enemyName = "Default Enemy Name";
TestGameObject_002 monoBehaviour = null;
public Enemy(TestGameObject_002 monoBehaviour)
{
}
}
public class TestGameObject_002 : MonoBehaviour
{
Enemy enemy = null;
void Start()
{
enemy = new Enemy(this);
StartCoroutine(TestCoroutine());
}
public IEnumerator TestCoroutine()
{
if(enemy != null) Debug.Log("TestGameObject_002.TestCoroutine(), enemy is not null");
else Debug.Log("TestGameObject_002.TestCoroutine(), enemy is null");
Debug.Log("TestGameObject_002.enemy.enemyName = " + enemy.enemyName);
yield return null;
}
}
Как и ожидалось, приведенный ниже код должен работать нормально.
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
class Enemy
{
public string enemyName = "Default Enemy Name";
TestGameObject_002 monoBehaviour = null;
public Enemy(TestGameObject_002 monoBehaviour)
{
this.monoBehaviour = monoBehaviour;
monoBehaviour.StartCoroutine(monoBehaviour.TestCoroutine());
}
}
public class TestGameObject_002 : MonoBehaviour
{
Enemy enemy = null;
void Start()
{
enemy = new Enemy(this);
}
public IEnumerator TestCoroutine()
{
//yield return null;
if(enemy != null) Debug.Log("TestGameObject_002.TestCoroutine(), enemy is not null");
else Debug.Log("TestGameObject_002.TestCoroutine(), enemy is null");
Debug.Log("TestGameObject_002.enemy.enemyName = " + enemy.enemyName);
yield return null;
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... comes-null
Мобильная версия