c# - 由于其保护级别,公共(public)无效方法无法访问

标签 c# unity-game-engine

我正在尝试从另一个脚本访问方法,但收到保护级别错误。

尽管我将该方法公开

这是我想要访问的方法:

public void Shoot()
        {
            timer = 0f;
            gunAudio.Play ();
            gunLight.enabled = true;
            faceLight.enabled = true;

            gunParticles.Stop ();
            gunParticles.Play ();

            gunLine.enabled = true;
            gunLine.SetPosition (0, transform.position);

            shootRay.origin = transform.position;
            shootRay.direction = transform.forward;

            if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
            {
                EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();

                if(enemyHealth != null)
                {
                    enemyHealth.TakeDamage (damagePerShot, shootHit.point);
                }

                gunLine.SetPosition (1, shootHit.point);
            }

            else
            {

                gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
            }
       }

我正在尝试使用以下代码行从另一个脚本访问它:

if (hit.transform.name == "MyObjectName" )
      GameObject.Find("Gun").GetComponent<PlayerShooting>().Shoot();

我怎样才能做到这一点?

最佳答案

PlayerShooting 类应该是公共(public)的。

关于c# - 由于其保护级别,公共(public)无效方法无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479646/

相关文章:

c# - 将 ListBox 的 ItemsSource 设置为其包含类的属性

c# - 使用c#获取连接到我的热点的设备的MAC地址

unity-game-engine - 如何通过代码访问 RectTransform 的左、右、上、下位置?

unity-game-engine - 可编写脚本的对象作为 Sprite 的自定义版本

c# - 创下本地高分?

android - 为什么无法将 apk 上传到 google play console?

c# - 使用数据绑定(bind)的 C#/WPF 中具有确定/取消行为的对话框

c# - 如何通过 Entity Framework 存储可变二维数组?

c# - 字符串到数字的转换和组分隔符

node.js - 将 websocket 压缩与 uWebSockets.js 和 Websocket-Sharp 结合使用