Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# unity scripting

Discussion in 'Scripting' started by Samantha, Sep 14, 2005.

  1. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    A question about public variables... Isn't it bad practice to expose all variables? I know that declaring a public variable allows the value to be modified in the Inspector. But in my C# coding, I've always created properties if I wanted variables to be modified. Does Unity support variables? How about private variables within classes?

    I'm also wondering if there is a way to refer to an object by its name. For example if I had a box called myBox, and a sphere, could I include the following code in a script linked to the sphere?

    Code (csharp):
    1. this.transform = myBox.transform;
    What is the proper way to refer to a different in-game object and its properties?
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Only public variables are visible in the inspector. (Private variables are shown in expert mode, read-only... which is nice when debugging.)
    Custom properties (with get and set functions) are not visible in the inspector.

    Game objects in Unity are not just single objects.

    Game objects consist of a game object containing component objects. The components attached to a game object are actually what define the behaviour of an object. Even the transform of a game object is a component.

    This is why you can attach more than one script object to a single game object.

    Please see the overview document in the Script Reference for a more detailed description on how to locate other objects and components.