
This week we imported some assets for our gems, distributed random gems around the environment and created a digging action and effect.
Importing Gems
To avoid the tedious work of getting up all the gem definitions, I created an asset pack on our Teams site containing:
- Gem models
- Gem images
- Completed gem definitions
- Updated GemDefinitions.cs
To avoid clashes, we deleted our existing gem definitions and GemDefinitions.cs script before importing the asset pack.
Random Gem
We wanted to create a prefab which represented a random gem. First though, we need to get the gem definitions into memory. We create new class called GemManager.cs and attach it to a new object in the scene, also called ‘Gem Manager’. GemManager.cs has an array for storing all the gem definitions and a single function to return one at random:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GemManager : MonoBehaviour
{
public GemDefinition[] GemDefinitions;
public GemDefinition RandomDefinition()
{
int index = Random.Range(0, GemDefinitions.Length);
return GemDefinitions[index];
}
}
In the scene we set the size of the array to 15 and added all our gem definitions to it.
We then created our RandomGem.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomGem : MonoBehaviour
{
public GemDefinition GemDefinition;
// Start is called before the first frame update
void Start()
{
GemManager gm = FindObjectOfType<GemManager>();
if (gm != null)
{
GemDefinition = gm.RandomDefinition();
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.white;
Gizmos.DrawWireSphere(transform.position, 0.5f);
}
}
This class looks for a GemManager in the scene when it starts and, assuming it does, it asks it for a random gem definition and stores that in its GemDefinition property. We also added an OnDrawGizmos() so we could see, in the Scene View, where the gems have been scattered.
Updating ItemScatterer
All we needed to do was to set Item in the to new random gem prefab and removing ActivateNearest(), as RandomGem doesn’t have Activate()/Deactivate() functions, which our lamp post did.
Adding a New Player Action: Digging
To see the existing user actions that had been defined, we navigated to the Player Capsule object and looked at the PlayerInput component. It references an asset called StarterAsset which, when we double-click on it, opens a new editor window. We use the “+” button next to Actions to defined a new action called “Dig”. Next to “Dig” we use the “+” to defined two new bindings; one to the ‘X’ key on the keyboard and one to the “Button West” button on the gamepad.

To respond to this new action, we create a new script called PlayerDig.cs, containing a function called OnDig(), and attached it to the “Player Capsule” game object – it has to be on the same game object as the PlayerInput component. Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerDig : MonoBehaviour
{
public void OnDig()
{
Debug.Log("Player hit dig");
}
}
In Play mode, we can see now that every time we hit the “X” key the message “Player hit dig” is written to the console.
Creating a Dust Particle Effect
On our Teams site there is a small image file called “dustmote_alpha.png”. We added it to our project in the ‘Sprites’ folder. Selecting it, we updated the properties in the inspector and selected “Alpha is transparency” (to indicate that the image has transparent portions) and hit the “Apply” button to save the change.
We created a new material, in our “Materials” folder, called “Dust Mote”. We needed to change the rendering mode to “Cutout” and assign our “dustmote_alpha.png” image to the texture box for the Albedo channnel:

We then created a new prefab with a particle effect on it. The settings are too numerous to list here, but you should examine it in the project if interested. Our material above provides the appearance of the particles.
We also added an audio source to this prefab and downloaded a digging sound effect from our Teams site to attach to it.
Finally we developed a small class called SelfDestruct.cs and attached it to the prefab:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelfDestruct : MonoBehaviour
{
public float SelfDestructInSeconds;
// Start is called before the first frame update
void Start()
{
Destroy(gameObject, SelfDestructInSeconds);
}
}
This little component means that we can create the dust effect and it will clean itself up after the sound and the particle effect have finished (about 3 seconds).
Code Download
The code for this week’s project is on our GitHub, as always.