Tag Archives: scripting

Input: Detect Mouseover

  mouseover Unity has events that tell your scripts when the mouse cursor hovers over an object and stops hovering over an object. This can be great for UI or for prompting the player that they they can interact with something.

OnMouseEnter()

The <span style="font-size: 12pt;">OnMouseEnter()</span> method will run when the mouse cursor first enters the object’s collider area (the object must have a collider for this to work). [code languge=”csharp”] void OnMouseEnter() { // do something, e.g. change the sprite or make a sound } [/code]

OnMouseExit()

The reverse method to <span style="font-size: 12pt;">OnMouseEnter()</span>, <span style="font-size: 12pt;">OnMouseExit()</span> is called when the mouse cursor exits the objects collider area. [code languge=”csharp”] void OnMouseExit() { // do something, e.g. change the sprite or make a sound } [/code]

Sample Script

This following script contains an example of what you can do with this functionality. Add this script to any object with the following:
  • A collider
  • A SpriteRenderer with a default sprite
  • Add a second sprite to the mouseOverSprite inspector field (the SpriteRenderer will show this sprite when the mouse cursor is over the GameObject).
[code language=”csharp”] using UnityEngine; using System.Collections; public class MouseOver : MonoBehaviour { [SerializeField] private Sprite mouseOverSprite; private Sprite defaultSprite; private SpriteRenderer sRenderer; void Start () { defaultSprite = gameObject.GetComponent&amp;lt;SpriteRenderer&amp;gt;().sprite; sRenderer = gameObject.GetComponent&amp;lt;SpriteRenderer&amp;gt;(); } void OnMouseEnter() { sRenderer.sprite = mouseOverSprite; } void OnMouseExit() { sRenderer.sprite = defaultSprite; } } [/code]

Expanding This

The Unity documentation has more details of this functionality (http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html). You can get inspiration for how to use this from the games you play. For games with a lot of UI this technique can bring those menus to life. You can do all kinds of transform tricks with it, such as:
  • Adjust an object’s scale
  • Add a slight rotation
  • Make a subtle sound
  • Animate the object.

Unity Scripting Primer Part 1: Public and Serialized Variables

We’ve all asked for help online and been given a succinct answer that, while probably 100% accurate, wasn’t any help at all because the helper assumed we knew something we didn’t. So frustrating! In these posts I’ll cover the most simple, basic building blocks of Unity scripting without making too many assumptions, and I’ll be glad to help out if anything I present here doesn’t live up to that goal.

Assumptions

I will have to make some assumptions:
  • You know the basics of using Unity - creating scripts, what a prefab is, etc. Follow the basic tutorials available at Unity3D.com if you need to.
  • You’re using C# for scripting and know the language basics (though most of this content is theoretical, so it applies to JavaScript/Boo as well).
  • You understand general programming/scripting terminology (variable, method, etc.).

Public and Serialized Fields in the Inspector

In Unity scripts, public variables (or fields) are accessible via the Unity inspector. So, if you have: [code language=”csharp”] public string Name; public bool BoolVariable; [/code] you will see something like this in the Inspector: UnityPublicVariables AM You can modify these public values in the editor while working on your game. And of course, being public, these variables can be accessed by other scripts in your game too.

Serialize don’t Publicize

BUT! Using a lot of public variables is poor practice, so another way to achieve the same goal without making variables public is to serialize the variables like this: [code language=”csharp”] [SerializeField] private string Name; [SerializeField] private bool BoolVariable; [/code] The above fields are private, but also accessible in the Unity inspector: UnityPublicVariables AM
HINT: For Boolean variables, a tick means ‘true’ and no tick is ‘false’. HINT: You can change public variables in real-time as you debug your game in the Unity editor, which lets you see the effect of different values.
You can even use lists of objects, such as: [code language=”csharp”] public List<Transform>Names; [/code] And in the designer you can set the number of list items and each value in the list: UnityPublicVariables2 Remember to still set the values of these public or serialized fields (either in the Inspector or via a script) or you’ll get an exception when you try to access the (null) values. You must include the relevant namespace at the top of your script to use lists: [code language=”csharp”] using System.Collections.Generic; [/code]

Public Unity Objects

It’s very useful to use public variables to hold references to gameobjects, transforms, etc. When you do this, you can drag-and-drop items from your scene into the variable’s public value. For example, if you want to reference a specific GameObject in your script, you can make a public or serialized field: [code language=”csharp”] public GameObject MyGmeObject; [/code] …then drag-and-drop any game object from your scene into the empty field. Your script can now access that game object via the MyGameObject variable. This is great for improving workflow. Of course you can use a list of GameObjects and drag-and-drop multiple objects into that list, such as a list of UI buttons or enemy spaceships. Unity is also clever enough to infer the correct component if required. For example, if you have a public Transform variable and drag a GameObject onto it in the editor, Unity will know to grab the GameObject’s transform to put into the public variable. This makes it much less fiddly than it could be.

Serialization

You can make custom classes editable in the Unity editor too. Simply add [System.Serializable] before the class definition, like this: [code language=”csharp”] [System.Serializable] public class Car { public string model; public string colour; } [/code] UnityVars3  

That’s It

That’s how public and serialized variables work in Unity’s Inspector! Below is the full text for a script using the above examples. You can attach this script to any game object in your Unity project to check it out. One way to do this is:
  1. Create a new game object (Unity Menu > GameObject > Create Empty)
  2. Select the new game object in the Hierarchy .
  3. In the Inspector, click Add Component.
  4. Select New Script.
  5. Change the script’s name to DemoScript*.
  6. Click Create and Add.
  7. Double-click the script name in the Inspector to opens the script in your script editor.
  8. Delete all the current script code (Ctrl-A, Del)
  9. Copy and paste the script code below into the empty script.
  10. Save the script.
  11. Go back to Unity, where the public variables are now available to edit in the Inspector.
* In Unity, the script’s filename must match the class name. In the code below the class name is DemoScript, so the filename must also be DemoScript. You can of course change the name, but must change both the filename and the class name.

Demo Public Variables Script

[code language=”csharp”] using UnityEngine; using System.Collections; using System.Collections.Generic; public class DemoScript : MonoBehaviour { public string Name; public bool BoolVariable; public List TransformList; [System.Serializable] public class Car { public string model; public string colour; } public Car MyPublicCar; // the rest of your code goes here! // you need to DO something with all those variables! } [/code] That’s the basics of how Unity handles variables in ways specific to creating games easily. In Part 2 I will explain the MonoBehaviour class’s special methods such as Update() and Awake(), and how they make game-specific functionality easy to handle.