Eggys Games

News Archive for ‘Game Making Programming’

C# Make a Basic Array

Lets say we want to make a basic array with a list of items or weapons etc. Lets put them in an array so we can loop through them when we need too.

Start off by declaring it

//Declare Array
public string[] eggysarray;

Then of course here are the basics to setting it and changing it

void Start () {

        ///Set Array
        eggysarray = new string[] { "this", "is", "an", "array"};

        //Change individual array item
        eggysarray[1] = "isnt";

        //returned "this"
        Debug.Log (eggysarray[0]);

Leave a comment Posted in Game Making Programming by Eggy on

Unity - C# Changing Between Lots of Sprites on One Object

Sometimes in a game you're going to want to have a lot of sprites on one object to make it easier to switch between them all. Lets take my Zombie dying for example, we want a bunch of gore to randomly come out of him, but we don't want to make 30 different little prefabs for each one do we? So lets make an array and make it quick and easier. 

First we make an array. Lets call it Mysprites. 

public Sprite[] Mysprites;

Now we have an array so in our inspector when we drag this code onto a prefab. It will allow us to set an array and drag out sprites onto it. Like so :

Now we have a lot of them ready to change. So on start we can do this :

roll = Random.Range (min, max);

GetComponent<spriterenderer> ().sprite = Mysprites [roll];

As we can see here we have a variable Roll that we run through 2 variables. You just need to declare them as ints of however many sprites youre going to use. 

Then it sets the spriteRenderer to display the sprite that it randomized.

There you have it, you can drag this script onto any object and be able to customize which random sprites it spawns, if you don't want it randomized you can customize it too display whichever sprite in the array you want when needed. Like for guns, ui, and more.

Leave a comment Posted in Game Making Programming by Eggy on

Unity - C# Class for doing a FadeOut Remove

To save performance you're going to have to remove objects. However having things simply dissapear sometimes is not a good look for the players. A good solution to this is a fadeout. You see it very commonly for mass produced decals such as bulletholes. 

To remove an object instantly you can simply use 

Destroy (gameObject);

But if we want to get a bit more fancy and do the fade out, here is a class you can simply drag and drop onto any sprite you are using in unity to make it fadeout. In the inspector you can set the time until it will dissapear. 

As you can see, first we have to get a reference to the sprite component. We then use Unitys colour class to make a reference to it's alpha property.

Once the countdown you have set in the inspector reaches 0 it will fade out. You can change the speed of its fade next to colourfade however I felt .03 was a good speed, this means it will dissapear in about a second. 

public class DestroyFadeOut : MonoBehaviour {

    public float deathremovetimer;

    private Color mycolour; 
    private float colourfade = 1;

    void Start () {
        mycolour = GetComponent<spriterenderer> ().color;
        mycolour.a = 1f;
    void Update () {

            deathremovetimer -= Time.deltaTime;

            if (deathremovetimer <= 0) {
                colourfade -= 0.03f;
                mycolour.a = colourfade;
                GetComponent<spriterenderer> ().color = mycolour;
                if (colourfade <= 0) {
                    Destroy (gameObject);


Here we can see how the fadeOut will look

Hope you find this class useful.

Leave a comment Posted in Game Making Programming by Eggy on

Unity - Basic Collisions in C#

In Unity the first thing you're gonna want to be using is collisions. This is what happens when 2 objects collide with each other, or rather two rigidbodies with colliders. Below is all you will need to get started. By adding a rigidbody and a box collider our object can now hit other objects in the scene.

Below I've included the functions that Unity uses to test for collision between objects. Simple copy and paste and then write inside the code you want to execute when these objects collide.

You will see their is triggers and normal. The difference with trigger is that the object will not collide with it in physics space, however it will still execute a hit-test. You use triggers on objects in the background like levers, scenery, doors etc. Where as normal collisions are for enemies and the player. 

These are the two components you need on an object to run a hit-test

Use This code below when you want a hit test between two objects

 //Basic Collision 
 void OnCollisionEnter2D (Collision2D col) {
    if (col.gameObject.tag == "NameOfObject") {
         ///Run Code 

 //Collision Exits
 void OnCollisionExit2D (Collision2D col) {
    if (col.gameObject.tag == "NameOfObject") {
         ///Run Code 

 //Non-Physics Collision
 void OnTriggerEnter2D (Collider2D col) {

 //Non-Physics Collision Exit
 void OnTriggerExit2D (Collider2D col) {

Leave a comment Posted in Game Making Programming by Eggy on