Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Arcsharp · Apr 01, 2019 at 12:33 AM · encapsulationsetters

How can I make this setter accessor only accept values that come from certain scripts?

So I've been trying to grasp encapsulation and I've simplified to the point its no where near as irritating as trying to do it on the project I'm currently working on so instead I made a test project where a button simply changes a variable. The problem is there's nothing to stop the value from coming from any where. If I had to describe it the situation is like a pipe that starts out very restrictive only to be accessible by everything which really just ends up being like why have the piping the first place? Here's the code I got with an example illustrating what will happen if some other script uses it.

 public class GameManager : MonoBehaviour
 {
     //I'm sorta secure? ◔_◔
     private static bool _ChangeMe = false;
 
     public static void SetChangeMe(bool value)
     {
         //Lets just accept whatever I get! ◕ ◡ ◕
         _ChangeMe = value;
     }
 
     // Update is called once per frame
     void Update()
     {
         //I'm just watching. Don't mind me. ( ͡° ͜ʖ ͡°) 
         Debug.Log("ChangeMe is: " + _ChangeMe);
     }
 }


 using UnityEngine;
 
 public class ChangeVariable : MonoBehaviour
 {
    public void ChangeIt()
     {
         //I hope some code else where doesn't completely screw this up. ಠ_ಠ
         GameManager.SetChangeMe(true);
     }
 }


 using UnityEngine;
 
 public class JoesPlan : MonoBehaviour
 {
     // Update is called once per frame
     void Update()
     {
         //Don't question Joe's plan. ^_-
         GameManager.SetChangeMe(false);
     }
 }

How would I prevent Joe's plan from messing up everything? I assume I would have to change something in the setter accessor but I have no clue how to determine where it got its value from.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Apr 01, 2019 at 04:11 AM

you cant really call it security because you are the only one writing the code changing the variables. there is no built in security from your self. normal practice would be if you don't want a particular script changing a variable you simply wouldn't put it in that script in the first place. anyways, the closest solution you are going to find is to require a second parameter in your function to include the name of the script it i's comming from.

     public static bool MakeitTrue(bool b,string script){
         if (script == "JoesScript") {
                         DevelopersAreWeird = b;
                 } else {
                         print ("you are not worthy");
                 }
     }
 
     public static bool DevelopersAreWeird;
 
 
 
     void Start () {        
 
         MakeitTrue (true, this.GetType ());}
Comment
Add comment · Show 1 · 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 RobAnthem · Apr 01, 2019 at 05:27 AM 0
Share

Could also do a caller/source reference in the wrapper function

 public static bool doThing(bool b, ref object caller)
 {
     DevelopersAreWeird = false;
     if (caller is SomeObjectType)
     {
         DevelopersAreWeird = b;
     }
     return DevelopersAreWeird;
 }

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

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Messaging system using C# 1 Answer

Getters/Setters with C# Array 1 Answer

What's the best way to hide classes when sharing C#? 2 Answers

HLAPI SyncVars property setter function 1 Answer

Modular Animation States or Encapsulating Animation States 0 Answers


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