Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法连接事件_C#_Events_Unity3d - Fatal编程技术网

C# 无法连接事件

C# 无法连接事件,c#,events,unity3d,C#,Events,Unity3d,我正在尝试将BasicLaser类中的事件安装到BlueShip类中,因此基本上每n个时间单位就会触发一次此事件。 这是与统一中的精灵“相连”的班级蓝调: using UnityEngine; using System.Collections.Generic; using System.Timers; using System; public class BlueShip : MonoBehaviour { enum LaserTypes { BASIC_LAS

我正在尝试将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);
                }
            }
        }
使用UnityEngine;
使用System.Collections.Generic;
使用系统计时器;
使用制度;
公共阶级蓝调:单一行为
{
枚举激光类型{BASIC_LASER};
专用浮子速度_u2;=100f;
私有列表=新列表();
私人游戏对象;
私有变换;
专用矢量3当前位置;
私有布尔值失效=真;
//用于初始化
void Start()
{
变换=变换;
/*基本激光是默认设置,保证在游戏开始时安装*/
laser.Add((游戏对象)Resources.Load(“BasicLaser”);
currentLaser=激光[(int)激光类型.基本激光];
laser[0].GetComponent().TimeRecursed+=新TimeRecursed(BasicLaser\u TimeRecursed);
//在这里,我向这个事件添加了观察者,但在BasicLaser类中查找名为*2的行
}  
public void BasicLaser_timerecursed(对象发送方,事件参数e)
{/*这应该对BasicLaser中复发的事件时间作出反应*/
IseFalsed=真;
}
//每帧调用一次更新
无效更新()
{
var amtToMove=Input.GetAxis(“水平”)*速度×时间.deltaTime;
变换.平移(Vector3.right*amtToMove);
/*下面这些设置了激光器的实例化位置*/
currentPosition_ux=变换位置.x;
currentPosition.y=transform.position.y+(this.transform.localScale.y/2)+currentLaser\u.transform.localScale.y;
currentPosition_uz=变换位置.z;
if(Input.GetKeyDown(KeyCode.UpArrow))
{
如果(已失效)
{
var t=实例化(激光类型[(int)激光类型.BASIC\u激光]、当前位置、四元数.identity);
IseFalsed=假;
}
}
}
}
//“连接”到统一的BasicLaser预制体的BasicLaser类
使用UnityEngine;
使用系统集合;
使用系统诊断;
使用制度;
公共委托无效时间重复(对象发送方,事件参数e);
公共类BasicLaser:单一行为
{
专用浮子速度_u2;=500f;
专用静态uint频率\=2;//此激光器可发射的频率
私有静态秒表秒表_u=新秒表();
公共事件timeRecursed timeRecursed;//这是事件
公共单位频率
{
获取{返回频率}
设置{frequency\=value;}
}
私有变换;
//用于初始化
void Start()
{
变换=变换;
秒表启动();
}
//每帧调用一次更新
无效更新()
{
var amtToMove=速度×时间.deltaTime;
变换.平移(Vector3.up*amtToMove);
var t=秒表运行时间(毫秒);
如果(t>频率)
{
秒表=新秒表();
秒表启动();
如果(timerecursed!=null)//*2,则始终为null!!!
虽然我在蓝调课上就迷上了,但发生了什么事?
{
timeRecursed(this,EventArgs.Empty);//引发事件
}
}
if(transform.position.y>=屏幕高度)
{
摧毁(游戏对象);
}
}
}

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

我认为这是你的问题:

(GameObject)Resources.Load("BasicLaser")

这将为您提供资产/资源文件夹中名为“BasicLaser”的对象,该对象(假设它存在)可能是一个预置。您可以在此处获得的
GameObject
与场景中的
GameObject
不同。您应该实例化此预置,然后附加到该实例化对象的事件,或者您应该通过将行更改为以下内容来引用场景中已经存在的
BasicLaser
laser_u2;.Add((GameObject)GameObject.Find(“BasicLaser”)
(如果带有
BasicLaser
组件的
游戏对象的名称是
“BasicLaser”
)。

呃,你的问题到底是什么?#TonyHopkinson的问题是,为什么我在BasicLaser中声明的TimeRecursed事件总是空的,即使我在BlueShip类中钩住了这个事件的观察者。抱歉,不清楚。您是否附加了调试器并完成了此操作?啊,明白了,所以+=似乎没有发生。第一个想法是确保GetComponent返回您认为是的实例,并检查您声明事件的方式,最后一个想法是其他一些代码不在某个地方。我知道,因为返回对象中的事件类型是我定义的,intellisense会检测到它。代码中肯定没有-=的地方。我在OP中提供的代码就是我得到的全部代码。