Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oen432 · Aug 02, 2015 at 02:29 AM · booleanpublic

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GiyomuGames · Aug 02, 2015 at 04:17 AM 0
Share

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.

avatar image oen432 · Aug 02, 2015 at 12:50 PM 0
Share

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.

avatar image Bonfire-Boy · Aug 02, 2015 at 01:28 PM 0
Share

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.

avatar image YoungDeveloper · Aug 02, 2015 at 04:19 PM 0
Share

@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.

avatar image Bonfire-Boy · Aug 02, 2015 at 09:13 PM 0
Share

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.

avatar image
0

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Access a variable from another script in update function 1 Answer

public static bool not working at all. 0 Answers

Help With C# Static Boolean! 2 Answers

Script not responding to public variable change 1 Answer

FindWithTag or how to find if there any instances left 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges