using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PieceMove : MonoBehaviour
{
Rigidbody2D enemy;
[SerializeField]
private int EnemyForce;
// Start is called before the first frame update
void Start()
{
enemy = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
private void Awake()
{
Messenger.PlusListener("isMoveStart", isMoveStart);
}
private void OnDestroy()
{
Messenger.RemoveListener("isMoveStart", isMoveStart);
}
void Update()
{
}
public void isMoveStart()
{
enemy.velocity = new Vector2(-EnemyForce, 0);
}
}
При столкновении создается экземпляр,и с ним уже появляются ошибки начала движения.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Instantiator : MonoBehaviour
{
[SerializeField]
private GameObject controller;
[SerializeField]
private GameObject pieceInstantiator;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "EditorOnly")
{
Destroy(collision.gameObject.transform.parent.gameObject);
Instantiate(controller, pieceInstantiator.transform.position, Quaternion.identity);
Messenger.Sirena("isMoveStart");
}
}
}
Выдаётся ошибка:NullReferenceException: Object reference not set to an instance of an object
PieceMove.isMoveStart () (at Assets/Scripts/PieceMove.cs:30)
Messenger.Broadcast (System.String eventType, MessengerMode mode) (at Assets/Scripts/Messenger.cs:175)
Messenger.Broadcast (System.String eventType) (at Assets/Scripts/Messenger.cs:161)
Instantiator.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Scripts/Instantiator.cs:31)