> For the complete documentation index, see [llms.txt](https://doc.realvirtual.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.realvirtual.io/components-and-scripts/defining-logic/unity-scripting.md).

# Unity Scripting

Programming Unity with C#

Unity Scripting is done with C# scripts. All realvirtual.io functionality is done based on C# Scripting. The possibilities are endless. As an editor you need to have a C# editor like Visual Studio or Rider installed. You can find a lot of information about C# and C# scripting on the Internet. For a full documentation of the Unity API please go to this page:

{% embed url="<https://docs.unity3d.com/Manual/ScriptingSection.html>" %}

If you are already a programmer you might prefer to use C# scripts instead of the visual scripting approaches like [Logicsteps](/components-and-scripts/defining-logic/logicsteps.md), [Unity Visual Scripting](/components-and-scripts/defining-logic/unity-visual-scripting.md) or [https://github.com/game4automation/doc/blob/doc/components-and-scripts/defining-logic/broken-reference/README.md](https://github.com/game4automation/doc/blob/doc/components-and-scripts/defining-logic/broken-reference/README.md "mention").

## Installation of Visual Studio, Rider or another Scripting environment

Before scripting in C# you need to install Visual Studio or Rider. Please check the documentation on the Unity website about installing Visual Studio:

{% embed url="<https://docs.unity3d.com/Manual/VisualStudioIntegration.html>" %}

{% hint style="info" %}
Please make sure, that your scripting environment is linked as external tool like shown below:
{% endhint %}

<figure><img src="https://260262196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpYxFg97YnJX96UzNNTSd%2Fuploads%2Fgit-blob-588a1c694d9833079cd01a2f802b7876a9b69dc5%2FVS-external-tools.png?alt=media" alt=""><figcaption></figcaption></figure>

## Scripting realvirtual.io with C\#

Most important thing is, if you wan to use realvirtual.io classes and properties, to embed it by putting this using in front of your code (or alternatively put your code into the

`using game4automation;`

Here is a small example of a PLC like script for controlling a Drive based on a Sensor:

```csharp
    using realvirtual;
    using UnityEngine;
    
    //! PLC Script for the game4automation demo model
    public class PLC_CanConveyor : ControlLogic
    {
        public bool On = true;

        [Header("References")] public PLCOutputBool StartConveyor;
        public PLCInputBool SensorOccupied;
        public PLCInputBool ButtonConveyorOn;
        public PLCOutputBool LampCanAtPosition;

        // Call this when all Updates are done
        void FixedUpdate()
        {

            if (SensorOccupied.Value == false && On && ButtonConveyorOn.Value == true)
            {
                StartConveyor.Value = true;
            }
            else
            {
                StartConveyor.Value = false;
            }

            LampCanAtPosition.Value = SensorOccupied.Value;
        }
    }

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://doc.realvirtual.io/components-and-scripts/defining-logic/unity-scripting.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
