我正在尝试从另一个脚本访问方法,但收到保护级别错误。
尽管我将该方法公开。
这是我想要访问的方法:
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/