Problems with "Introducing Encapsulation"

Hello! I cannot get my script to work after Lecture 108, Introducing Encapsulation of the Complete C# Unity Game Developer 3D - Archived Course.
My problem is in this error:
Assets\Scripts\Enemy.cs(28,20): error CS0122: ‘ScoreBoard.ScoreHit(int)’ is inaccessible due to its protection level
Here is my Enemy script:

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

public class Enemy : MonoBehaviour
{

    [SerializeField] GameObject deathFX;
    [SerializeField] Transform parent;
    [SerializeField] int scorePerHit = 12;

    ScoreBoard scoreBoard;

    // Start is called before the first frame update
    void Start()
    {
        scoreBoard = FindObjectOfType<ScoreBoard>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnParticleCollision(GameObject other)
    {
        scoreBoard.ScoreHit(scorePerHit);
        GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
        fx.transform.parent = parent;
        Destroy(gameObject);
    }

}

And here is my ScoreBoard script:

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

public class ScoreBoard : MonoBehaviour
{


    int score = 0;
    Text scoreText;

    // Start is called before the first frame update
    void Start()
    {
        scoreText = GetComponent<Text>();
        scoreText.text = score.ToString();
    }

 

    private void ScoreHit(int scoreIncrease)
    {
        score = score + scoreIncrease;
        scoreText.text = score.ToString();
    }
}

I think the problem may have to do with the ScoreBoard in the Enemy Script is not referencing ScoreBoard.cs script. I don’t really understand the problem, but I think this is what is wrong, but Unity says

'ScoreBoard.ScoreHit(int)’ is inaccessible due to its protection level

Help would be greatly appreciated!!
Thank you!

Change to word private to public so it becomes:

public void ScoreHit(int scoreIncrease)

private means the method can only be accessed by methods within the ScoreBoard class. public allows to classes and methods outside ScoreBoard to also be able call those methods.

1 Like

I didn’t catch this! Thank you so much! This means a lot to me!

1 Like

Glad I could help.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms