- Home /
Public boolean can't be accessed due to its protection level.
Hello everyone,
I have two scripts, first has public bool isDead. In the second script I want to check if isDead is true or false. And that's how I do that.
void Update(){
if(PlayerHealth.isDead){
//code
}
}
PlayerHealth is the name of the script. And what do I get?
PlayerHealth.isDead is inaccessible due to its protection level
These two scripts are inside diffrent Objects.
Answer by YoungDeveloper · Aug 02, 2015 at 02:31 AM
You are accessing isDead like a static variable. Use Getcomponent from gameobject which holds that playerscript instance or set is isDead static, which i don't suggest as its player, and is not statically living for the whole software life time.
I explain it pretty clearly in these two topics:
http://answers.unity3d.com/questions/550578/cant-understand-getcomponent-c.html
http://answers.unity3d.com/questions/597617/how-do-i-addsubtract-variables-between-two-differe.html
Yes, so basically you need to write GetComponent<PlayerHealth>().isDead
You should also learn a bit more about object oriented languages and and public, protected, private, static variables.
I sloved that problem in other way, just connected this two scripts, anyway, I used GetComponent many times, I know how it works but still, same error appears. $$anonymous$$y problem is solved so it's not need continue this topic. Thanks anyway.
There is actually nothing in the code shown that indicates that they are accessing the variable statically. PlayerHealth (in the code snippet) could be a reference to a PlayerHealth instance.
And can you explain why they were getting an error about the variable's protection level? If the issue was do with accessing it like a static variable, that would produce an error telling them that they need to access it through an instance.
I think there was something else going on here.
@Bonfire That's why variable na$$anonymous$$g conventions exist, op clearly has to do research on all topics. Technically he's not showing class declaration either or inheritance from mono (update wont be executed, even if no errors), we assume it's there.
Not sure what you mean about na$$anonymous$$g conventions. There are many different ways of na$$anonymous$$g variables. I've known people who give variables exactly the same name as their class as a matter of course. It works fine (although I did find it confusing when I first tried to help with their code). You simply can't assume something's a class rather than an instance on the basis of its spelling.
The point is, in this case, the idea that its a static reference does not appear to hold water because if it were, they would be getting a different error message.
So yeah, you're right that we're not being shown the code that matters. I wouldn't even have attempted to provide a solution to their problem without it.
Answer by mtdrume · Aug 02, 2015 at 06:25 PM
I would do this:
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public static PlayerHealth current;
public bool isDead;
void Start ()
{
current = this;
}
}
You can now access isDead wherever you like calling:
PlayerHealth.current.isDead
Also, current can say whatever you want it to say :)