Why cant I access a PUBLIC function (Cs, VS2015 and 2D)? Help?
So, I have this script called "Player":
void Update() {
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector2.up * speed * Time.deltaTime);
GetComponent<Rigidbody>().useGravity = false;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Vector2.down * speed * Time.deltaTime);
}
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
public void Die(){
SceneManager.GetActiveScene();
}
}
And this one called "Missile":
void Collision(Collision col)
{
if (col.gameObject.name == "Player")
{
GetComponent<Player>().Die();
}
}
}
I can't access Die() function through Missile although Die() is public. It shows me this error: `Player.Die()' is inaccessible due to its protection level
Comment
Best Answer
Answer by taylank · Jan 13, 2016 at 08:55 PM
Seems to me you are checking for the collision on the Missile object, and the Die method is on the player, correct? If the missile is hitting the player and you want to access the Player component on the player object, you'd replace
GetComponent<Player>().Die(); //this looks for the component on the missile object
with
col.collider.GetComponent<Player>().Die(); //looks for the component on the collided object
Also make sure the Player class itself is public.