我正在尝试从另一个脚本访问一个方法,但我收到了保护级别错误。
即使我将方法公开。
这是我想要访问的方法:
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();
我该如何做到这一点?
答案 0 :(得分:1)
PlayerShooting
课程应公开。
答案 1 :(得分:1)
尝试重新导入PlayerShooting.cs。如果没有任何帮助,请关闭Unity,扔掉Library文件夹,然后再打开它。如果问题仍然存在,则会发生命名冲突,意外重复类声明或类似情况。