Почему получается использовать другой скрипт, как класс в скрипте только, если он не наследуется от MonoBehaviour?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Script2 : MonoBehaviour {
…
Script1 a = new Script1
…
}
Если Script1 наследуется от MonoBehaviour, то выдаёт ошибку.
Update: Я пишу реализацию NEAT на C#, поэтому каждой связи, нейрону приходится вести отдельные списки и создать классы, которые изначально унаследованы от MonoBehaviour
Вот три скрипта с которыми проблема:
main1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class main1 : MonoBehaviour
{
// Start is called before the first frame update
public ComponentScript a;
public NewScript b;
void Start()
{
a = gameObject.AddComponent<ComponentScript>();
b = new NewScript();
}
// Update is called once per frame
void Update()
{
b.work();
a.work();
}
}
ComponentScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComponentScript : MonoBehaviour
{
// Start is called before the first frame update
public int a = 42;
void Start()
{
Debug.Log("Component Start");
}
// Update is called once per frame
void Update()
{
Debug.Log("Component Update");
}
public void work(){
Debug.Log("Component work " + a);
}
}
NewScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewScript : MonoBehaviour
{
// Start is called before the first frame update
public int a = 52;
void Start()
{
Debug.Log("New Start");
}
// Update is called once per frame
void Update()
{
Debug.Log("New Update");
}
public void work(){
Debug.Log("New Work" + a);
}
}
Я нашёл три вещи:
1. AddComponent забивает инспектор.
2. Через new будет работать, но без Start() и Update(), по вызову функции работать будут.
3. Декларировать нужно тоже будет в Start(), по умолчанию нельзя через new нельзя или NullException.