Documentation

Support

Unity Lite

Open Unity Lite

Unity Lite

Introduction to variables, methods, and parameters

Use methods, variables, and parameters to store data, perform actions, and control the flow of your Logic scripts.
Read time 2 minutesLast updated 2 days ago

Variables

A variable is a container you can use to store information like numbers, text, colors, or references to objects to use in your Logic scripts. For example:
  • You operate a solar-plus-battery site.
  • You create a variable named
    BatteryStateOfCharge
    to store the battery’s current charge level.
  • BatteryStateOfCharge
    is a number that changes as the battery charges and discharges.
  • You make
    BatteryStateOfCharge
    a
    float
    type to hold decimal values for precise control.
  • When sensors report a new reading, you update
    BatteryStateOfCharge
    . If it drops below a threshold, your Logic triggers an alert.
For instructions on how to create a variable, refer to Create a variable.

Methods

Use methods to group a set of actions together that perform a specific task. Methods can also help keep your Logic scripts modular and organized. For example:
  • You have a door in your scene that you want to open when the user clicks on it.
  • You create a method called
    OpenDoor
    that contains the logic to open the door:
    • Animate the door object as it opens.
    • Play a creaking sound.
  • Then, whenever you want to open the door in your logic, you just call the
    OpenDoor
    method instead of rewriting the same logic over and over again.
For instructions on how to create a method, refer to Create a method.

Return values

A method can return a value after it runs. This is useful when you want to get some information back from the method. For example, with the
OpenDoor
example, you might want to know when the door finishes opening. You can modify the method to return a true/false value, which returns
true
when the door is open.
In this case, the return type of the method is a Bool value. However, if you don't want to return any value, you can set the return type to Void. The method performs the action but doesn't return anything. For more information about return types, refer to New Method window reference.

Use methods in your logic

If you create a method with a return type other than Void, you can drag the method node from the right panel into the Logic Editor.

Parameters

A parameter is a variable that you pass into a method when you use it. Parameters let you reuse the same method but with different inputs each time. For example:
  • You want to create a notification that shows the user different messages.
  • You create a method called
    ShowNotification
    and give it a parameter called
    Message
    of type
    String
    .
  • You give the method the following logic:
    • Display the
      Message
      text on the screen.
    • Animate the UI element to slide in and out.
    • Play a notification sound.
  • Now, whenever you want to show a notification, you call the
    ShowNotification
    method and pass in the message you want to display:
    • You have a new message.
    • An error occurred.
This way, you can use the same method to show different messages without rewriting the logic each time. You can create parameters when you create a method. For information about parameter properties, refer to Parameter details.