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.