Skip to main content
Tweeted twitter.com/StackGameDev/status/897519790956400640
added 12 characters in body
Source Link
Jacob
  • 2.6k
  • 3
  • 19
  • 50

Let's say I have a variable named button of type Button which is of course System.Serializable.

I want that variable to be assignable through the inspector, so I make it public:

public class SomeScript : MonoBehaviour{
    public Button button;
}

Which gives me that:

enter image description here

Now, I want to keep the code clean & secure (organized) and there's a real reason why I need to preserve the encapsulation.

I know that there are multiple ways of retrieving components on runtime like transform.Find("Some/Genius/TypeUnsafe/Path") or GameObject.Find("3 seconds scene iteration").GetComponent(...). I want to avoid that.

How do I mix a variable private access with it's public access in the inspector?

Let's say I have a variable named button of type Button which is of course System.Serializable.

I want that variable to be assignable through the inspector, so I make it public:

public class SomeScript : MonoBehaviour{
    public Button button;
}

Which gives me that:

enter image description here

Now, I want to keep the code clean & secure and there's a real reason why I need to preserve the encapsulation.

I know that there are multiple ways of retrieving components on runtime like transform.Find("Some/Genius/TypeUnsafe/Path") or GameObject.Find("3 seconds scene iteration").GetComponent(...). I want to avoid that.

How do I mix a variable private access with it's public access in the inspector?

Let's say I have a variable named button of type Button which is of course System.Serializable.

I want that variable to be assignable through the inspector, so I make it public:

public class SomeScript : MonoBehaviour{
    public Button button;
}

Which gives me that:

enter image description here

Now, I want to keep the code clean & secure (organized) and there's a real reason why I need to preserve the encapsulation.

I know that there are multiple ways of retrieving components on runtime like transform.Find("Some/Genius/TypeUnsafe/Path") or GameObject.Find("3 seconds scene iteration").GetComponent(...). I want to avoid that.

How do I mix a variable private access with it's public access in the inspector?

Source Link
Jacob
  • 2.6k
  • 3
  • 19
  • 50

How to make a field visible (attachable) in the inspector but inaccessible through code (private-like)?

Let's say I have a variable named button of type Button which is of course System.Serializable.

I want that variable to be assignable through the inspector, so I make it public:

public class SomeScript : MonoBehaviour{
    public Button button;
}

Which gives me that:

enter image description here

Now, I want to keep the code clean & secure and there's a real reason why I need to preserve the encapsulation.

I know that there are multiple ways of retrieving components on runtime like transform.Find("Some/Genius/TypeUnsafe/Path") or GameObject.Find("3 seconds scene iteration").GetComponent(...). I want to avoid that.

How do I mix a variable private access with it's public access in the inspector?