无法挂接事件
本文关键字:事件 | 更新日期: 2023-09-27 18:35:31
我正在尝试将 BasicLaser 类的事件安装在 BlueShip 类中,所以基本上这个事件将每 n 个时间单位触发一次。
这是与精灵"连接"的蓝船类
using UnityEngine;
using System.Collections.Generic;
using System.Timers;
using System;
public class BlueShip : MonoBehaviour
{
enum LaserTypes { BASIC_LASER };
private float speed_ = 100f;
private List<GameObject> laser_ = new List<GameObject>();
private GameObject currentLaser_;
private Transform transform_;
private Vector3 currentPosition_;
private bool isElapsed_ = true;
// Use this for initialization
void Start()
{
transform_ = transform;
/*Basic laser is the default and it's guaranteed to be installed at the beginning of the game*/
laser_.Add((GameObject)Resources.Load("BasicLaser"));
currentLaser_ = laser_[(int)LaserTypes.BASIC_LASER];
laser_[0].GetComponent<BasicLaser>().timerElapsed += new TimerElapsed (BasicLaser_timerElapsed);
//HERE I'M adding observer to this event but look for line named *2 in class BasicLaser
}
public void BasicLaser_timerElapsed(object sender, EventArgs e)
{/*this is supposed to react to event timerElapsed in BasicLaser*/
isElapsed_ = true;
}
// Update is called once per frame
void Update()
{
var amtToMove = Input.GetAxis("Horizontal") * speed_ * Time.deltaTime;
transform_.Translate(Vector3.right * amtToMove);
/*Those below set the position for laser to be instantiated at*/
currentPosition_.x = transform_.position.x;
currentPosition_.y = transform_.position.y + (this.transform.localScale.y / 2) + currentLaser_.transform.localScale.y;
currentPosition_.z = transform_.position.z;
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (isElapsed_)
{
var t = Instantiate(laser_[(int)LaserTypes.BASIC_LASER], currentPosition_, Quaternion.identity);
isElapsed_ = false;
}
}
}
}
//BasicLaser class which is "connected" to a BasicLaser prefab in unity
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System;
public delegate void TimerElapsed(object sender,EventArgs e);
public class BasicLaser : MonoBehaviour
{
private float speed_ = 500f;
private static uint frequency_ = 2;//the frequency with which this laser can be fired
private static Stopwatch stopWatch_ = new Stopwatch();
public event TimerElapsed timerElapsed;//HERE IS THE EVENT
public uint Frequency
{
get { return frequency_; }
set { frequency_ = value; }
}
private Transform transform_;
// Use this for initialization
void Start()
{
transform_ = transform;
stopWatch_.Start();
}
// Update is called once per frame
void Update()
{
var amtToMove = speed_ * Time.deltaTime;
transform_.Translate(Vector3.up * amtToMove);
var t = stopWatch_.Elapsed.Milliseconds;
if (t > frequency_)
{
stopWatch_ = new Stopwatch();
stopWatch_.Start();
if (timerElapsed != null)//*2 THIS IS ALWAYS NULL!!!
even though I've hooked it in BlueShip class, what's going on?
{
timerElapsed(this, EventArgs.Empty);//raises the event
}
}
if (transform.position.y >= Screen.height)
{
Destroy(gameObject);
}
}
}
请检查控制台或日志文件,并告诉我们您是否在BlueShip
的Start
方法中遇到任何错误。 还可以使用 Debug.Log
并查看日志文件,以查看是否有任何对象在不应该为 null 时为 null。(例如 Debug.Log (currentLaser_);
)
我认为这是你的问题:
(GameObject)Resources.Load("BasicLaser")
这使您是资产/资源文件夹中名为"BasicLaser"的对象,它(假设它存在)可能是一个预制件。 您可以在此处获得GameObject
与场景中可能拥有的GameObject
不同。 您应该实例化此预制件,然后附加到该实例化对象的事件,或者您应该引用场景中已存在的BasicLaser
,方法是将行更改为以下内容:laser_.Add((GameObject)GameObject.Find ("BasicLaser"));
(如果具有BasicLaser
组件的GameObject
的名称为 "BasicLaser"
)。