Help with error CS0122: 'PlayerUI.UpdateText(string)' is inaccessible due to its protection level please

For some reason, the cs script is giving errors for (33, 26) and (24, 18). If anyone can help, it will be very apricated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInteract : MonoBehaviour
{
private Camera cam;
[SerializeField]
private float distance = 3f;
[SerializeField]
private LayerMask mask;
public PlayerUI playerUI;

// Start is called before the first frame update
void Start()
{
    cam = GetComponent<PlayerLook>().cam;
    playerUI = GetComponent<PlayerUI>();
}

// Update is called once per frame
void Update()
{
    playerUI.UpdateText (string.Empty);
    //ray at senter of screen.
    Ray ray = new Ray(cam.transform.position, cam.transform.forward);
    Debug.DrawRay(ray.origin, ray.direction * distance);
    RaycastHit hitInfo; // variable to store our collision info
    if (Physics.Raycast(ray, out hitInfo, distance, mask))
    {
        if (hitInfo.collider.GetComponent<Interactable>() != null)
        {
            playerUI.UpdateText(hitInfo.collider.GetComponent<Interactable>().promptMessage);
        }
    }
}

}

if you define a variable, class or a function it always has a modifier which sets its protection level. The 3 options are:

  • private
  • protected
  • public

unless you work with inheritance you can forget about protected for now.

If something is defined as private:

   private int myPrivateInt;

you can only use that variable in the same class.

if something is defined as public:

    public int myPublicInt;

    public void UpdateText(string arg) { .... }

then you can access that also from code which is not in the same class.

so in your case go to the script PlayerUI and change the access modifier on the function UpdateText to public.

If you do not define a modifier in front then it will always be default be private:

    int myInt;  //private by default

    void MyUpdateTextFunction() {} //also private by default