C# 由于其保护级别,无法访问Public void方法
我试图从另一个脚本访问一个方法,但是我得到了保护级别错误 即使我公开了这个方法 这是我要访问的方法:C# 由于其保护级别,无法访问Public void方法,c#,unity3d,C#,Unity3d,我试图从另一个脚本访问一个方法,但是我得到了保护级别错误 即使我公开了这个方法 这是我要访问的方法: public void Shoot() { timer = 0f; gunAudio.Play (); gunLight.enabled = true; faceLight.enabled = true; gunParticles.Stop ();
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);
}
}
public-void-shot()
{
定时器=0f;
guncaudio.Play();
gunLight.enabled=true;
faceLight.enabled=true;
停止();
枪弹。Play();
gunLine.enabled=true;
gunLine.SetPosition(0,变换位置);
shootRay.origin=transform.position;
shootRay.direction=transform.forward;
if(Physics.Raycast(射击射线、射出命中、射程、射击能力遮罩))
{
EnemyHealth-EnemyHealth=shootHit.collider.GetComponent();
if(enemyHealth!=null)
{
enemyHealth.TakeDamage(伤害Pershot,射击命中点);
}
瞄准线设定位置(1,射击命中点);
}
其他的
{
gunLine.SetPosition(1,shootRay.origin+shootRay.direction*范围);
}
}
我正在尝试使用以下代码行从另一个脚本访问它:
if (hit.transform.name == "MyObjectName" )
GameObject.Find("Gun").GetComponent<PlayerShooting>().Shoot();
if(hit.transform.name==“MyObjectName”)
GameObject.Find(“Gun”).GetComponent().Shoot();
我如何才能做到这一点?PlayerShooting类应该是公共的。尝试重新导入PlayerShooting.cs。如果没有任何帮助,关闭Unity,扔掉库文件夹,然后再次打开它。如果问题仍然存在,您将遇到命名冲突、意外重复的类声明或类似问题。要从其他脚本访问它,您需要将其设置为静态。我尝试将其设置为静态,但仍然得到相同的结果error@FᴀʀʜᴀɴAɴᴀᴍ 它不需要是静态的,因为他正在访问PlayerShooting的一个实例。我匆忙地读了这个问题。我只是错过了他访问该方法的代码的最后一部分。@FᴀʀʜᴀɴAɴᴀᴍ, 是的,一开始我被绊倒了,我不得不想一想。我刚刚为这段代码构建了一个测试,它对我来说运行良好。如果编译时错误不是来自
GetComponent()
,那么类型应该足够容易访问。但是,编译时错误可能来自于此。