无法挂钩活动

时间:2012-05-27 12:26:00

标签: c# events unity3d

我正在尝试在BlueShip类中安装BasicLaser类中的事件,所以基本上每n个时间单元都会触发此事件。
这是BlueShip类,它与一个精灵“连接”:

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);
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

请检查控制台或日志文件,并告诉我们您是否在BlueShip的{​​{1}}方法中出现任何错误。还可以使用Start并查看日志文件,以查看是否有任何对象在不应该出现时为空。 (例如Debug.Log

我认为这是你的问题:

Debug.Log (currentLaser_);

这会在Assets / Resources文件夹中为您提供名为“BasicLaser”的对象,该对象(假设它一直存在)可能是一个预制件。您可以在此处获得的(GameObject)Resources.Load("BasicLaser") 与您在场景中可能拥有的GameObject不同。您应该实例化此预制件然后附加到该实例化对象的事件,或者您应该引用场景中已存在的GameObject,方法是将行更改为:BasicLaser(如果laser_.Add((GameObject)GameObject.Find ("BasicLaser"));组件GameObject的名称为BasicLaser)。