Michael
Michael

Reputation: 135

Public void method is inaccessible due to its protection level

I am trying to access a method from another script, but I am getting the protection level error.

Even though I made the method Public.

This is the method I want to access:

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);
            }
       }

And I am trying to access it from another script with this line of code:

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

How can I make this work ?

Upvotes: 2

Views: 646

Answers (2)

Thomas Hilbert
Thomas Hilbert

Reputation: 3639

Try reimporting PlayerShooting.cs. If nothing helps, close Unity, throw away the Library folder, then opening it again. If problem still persists you are having naming collisions, accidental duplicate class declaration or similar.

Upvotes: 1

galdin
galdin

Reputation: 14034

The PlayerShooting class should be public.

Upvotes: 1

Related Questions