- Home /
error CS0122: `GameController.AddScore(int)' is inaccessible due to its protection level
Hello everyone. I'm trying to develop a script that add points when the player touch an object. I'm following the official unity tutorial but it seems to have problems. When I'm in the unity platform I always have this error:
error CS0122: `GameController.AddScore(int)' is inaccessible due to its protection level
Here is the two scripts used:
DestroyByContacts (applied to object that need to be destroyed after collision and add point:
using UnityEngine;
using System.Collections;
public class DestroyByContact : MonoBehaviour {
public int scoreValue;
private GameController gameController;
void Start() {
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null) {
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null) {
Debug.Log("Cannot find 'GameController' script");
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Player")
{
Destroy(this.gameObject);
gameController.AddScore(scoreValue);
}
}
}
GameController ( applied to the GameController Object of the scene:
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public GUIText scoreText;
private int score;
void Start ()
{
score = 0;
UpdateScore ();
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
}
void AddScore( int newScoreValue )
{
score += newScoreValue;
UpdateScore ();
}
}
Please help! I'm becoming crazy! I don't know how to adjust this error.
Answer by tanoshimi · Jun 06, 2014 at 06:41 PM
Pretty much as the error says, your AddScore method is not publicly accessible. Change
void AddScore( int newScoreValue)
To
public void AddScore(int newScoreValue)
@tanoshimi thanks for your help! It's goes right! But now when the player collide with the other objects the game stop and give me this error:
NullReferenceException: Object reference not set to an instance of an object
DestroyByContact.OnCollisionEnter2D (UnityEngine.Collision2D coll) (at Assets/Scripts/DestroyByContact.cs:24)
What is this error?
Your answer
![](https://koobas.hobune.stream/wayback/20220613150941im_/https://answers.unity.com/themes/thub/images/avi.jpg)