Author Archives: Patrick

Common Types and Errors in Unity

In this section, we will focus on the most common types of errors and bugs that may occur when coding in C # and Unity.

When coding with Unity, you will undoubtably come across errors, and it is part of the learning process; I have listed below some of the most common types of errors and bugs, along with how they manifest, and how they can be fixed. This way, you will find it easier to identify your errors, understand what they mean, and ultimately be able to fix errors and avoid them on the long run.

Null Reference Exception

  • Meaning of this error: Null reference exceptions typically manifest as runtime errors in the Console window, indicating that a variable or object being accessed is null.
  • How to fix it: To fix null reference exceptions, you need to ensure that variables are properly initialized before use and to perform null checks where necessary.
  • Preventive Actions: Initialize variables when declared, use null checks before accessing object properties or methods, and follow best practices for object instantiation and management.
  • Example:

//Before
GameObject player;

void Start()

{

    player.transform.position = Vector3.zero; // Error: player is null

}

// After:

GameObject player;

void Start()

{

    if (player != null)

        player.transform.position = Vector3.zero;

}

In the previous code, while using the player variable may trigger an error, we correct this code by checking that the player variable is not null before using it.

Logic Error

  • Meaning of this error: Logic errors result in unexpected behaviors during gameplay, such as incorrect AI behavior or game mechanics (e;g., an NPC that keeps walking into walls).
  • How to Fix it: To fix  logic errors, you need to review and correct the logic in the code to match the desired behavior.
  • Preventive Actions: Break down complex logic into smaller, testable parts, use comments to clarify intentions, and perform thorough testing.
  • Example:
// Before:

int health = 100;

void Update()

{

    if (health = 0) // Error: Assignment instead of comparison

    {

        Debug.Log("Player defeated!");

    }

}

// After:

int health = 100;

void Update()

{

    if (health == 0) // Comparison corrected

    {

        Debug.Log("Player defeated!");

    }

}

In the previous code, we make the mistake of using = for a comparison instead of ==; this is corrected in the second version.

Array Index Out of Bounds

  • Meaning of this error: Array index out of bounds errors occur when trying to access an array element at an invalid index, leading to runtime exceptions. Typically, we may want to access an index in an array that is beyond its size.
  • How to Fix it: Ensure that array indices are within bounds before accessing them.
  • Preventive Actions: Always check the length of arrays before accessing elements, validate user input, and use data structures that automatically handle bounds checking where possible.
  • Example:

// Before:

int[] numbers = new int[3];

void Start()

{

    int value = numbers[3]; // Error: Index out of bounds

}

// After:

int[] numbers = new int[3];

void Start()

{

    if (numbers.Length > 3) // Check array length before accessing

    {

        int value = numbers[3];

    }

}

In the previous code, we try to access the element at the index 3 for the array numbers; however, because this array has a size of 3, its elements would be at the indexes 0, 1, and 2, but not 3. Therefore, an error will be generated when accessing the 4th element at the index 3. To fix this issue, we check that the array has more than 3 elements (or at least 4) before trying to access the 4th element (at the index 3).

Infinite Loops

  • Meaning of this error/concept: Infinite loops cause the programme (or game) to become unresponsive because the condition to exit the loop is never true; this causes the programme to loop indefinitely and required players to force quit the game.
  • How to Fix it: Review loops and ensure there’s a condition for termination.
  • Preventive Actions: Double-check loop conditions, use break statements when necessary to exit loops, and perform thorough testing.
  • Example:
// Before:

void Update()

{

    while (true) // Error: Infinite loop

    {

        // Code that never exits loop

    }

}

// After:

void Update()

{

    int counter = 0;

    while (counter < 100) // Terminate condition added

    {

        // Code that eventually exits loop

        counter++;

    }

}

In the previous code, we initially create an infinite loop as the condition to exit the loop is always false; to fix this issue, we modify our loop to include an exit condition that we know will be reached at some stage (the value of the variable counter will reach 100 as it is increased by one every time).

Performance Bottlenecks

  • Impact of this bug: Performance bottlenecks result in decreased frame rate, stuttering, or lag during gameplay.
  • How to Fix it: Optimize code by reducing unnecessary calculations and use efficient resource management techniques.
  • Preventive Actions: Profile code regularly to identify performance bottlenecks, optimize critical sections, and follow best practices for performance-conscious development.
  • Example: (Continued below)
// Before:

void Update()

{

    for (int i = 0; i < 10000; i++)

    {

        Instantiate(prefab, transform.position, Quaternion.identity); // High instantiation frequency

    }

}

// After:

void Start()

{

    for (int i = 0; i < 10000; i++)

    {

        GameObject obj = Instantiate(prefab, transform.position, Quaternion.identity); // Instantiate once

        obj.SetActive(false); // Deactivate objects if needed

    }

}

In the previous code we instantiate a prefab 10000 times; however, this could be resource intensive; to fix this issue, we deactivate some the objects created if they are not used.

Memory Leaks:

  • Impact of this bug: Memory leaks result in a gradual increase in memory usage over time, potentially causing performance degradation or crashes.
  • How to Fix it: Identify and release unused resources or objects when they’re no longer needed.
  • Preventive Actions: Use Unity’s built-in memory profiler to detect leaks, ensure proper cleanup of resources, and follow best practices for memory management.
  • Example:
// Before:

List<GameObject> objects = new List<GameObject>();

void Update()

{

    GameObject obj = Instantiate(prefab);

    objects.Add(obj);

}

// After:

List<GameObject> objects = new List<GameObject>();

void Update()

{

    GameObject obj = Instantiate(prefab);

    objects.Add(obj);

    Destroy(obj); // Ensure to destroy objects when no longer needed

}

In the previous code, we create objects as the game progresses; however, this can start to use significant memory; so, to solve this issue, we make sure that we destroy the objects that are no longer needed.

Floating-Point Precision Errors

  • Impact of this bug: Floating-point precision errors result in inaccurate calculations or unexpected results due to limited precision of floating-point numbers.
  • How to Fix it: Use appropriate rounding techniques and avoid relying on precise equality checks for floating-point numbers.
  • Preventive Actions: Be aware of floating-point precision limitations, use Mathf.Approximately() for comparisons, and perform thorough testing with various input values.
  • Example:
// Before:

float result = 0.1f + 0.2f;

Debug.Log(result); // Might not print expected result due to floating-point precision

// After:

float result = Mathf.Approximately(0.3f, 0.1f + 0.2f);

Debug.Log(result); // Use Mathf.Approximately for floating-point comparisons

In the previous code, because comparing floating numbers using the = sign may be often inaccurate, we, instead, compare two float values (0.3f and 0.2f + 0.1f) by using the function Mathf.Approximately.

Concurrency Issues

  • Meaning and Impact of this bug: Concurrency issues lead to unexpected behaviors or data corruption when multiple processes or threads access shared resources concurrently (i.e., at the same time).
  • How to Fix it: Use synchronization mechanisms such as locks or mutexes to control access to shared resources.
  • Preventive Actions: Design code with concurrency in mind, avoid shared mutable state (where two or more entities have access to the same data) where possible, and use thread-safe data structures (i.e., structures that can function correctly and predictably when accessed by multiple threads concurrently).
  • Example:
// Before:

int counter = 0;

void Update()

{

    counter++;

    Debug.Log("Counter: " + counter);

}

// After:

int counter = 0;

void Update()

{

    // Use Unity's synchronization mechanism for thread-safe access

    Interlocked.Increment(ref counter);

    Debug.Log("Counter: " + counter);

}

In the previous code:

  • Before the change, we simply increment the counter using counter++, which is not thread-safe.
  • After the change: the code uses Interlocked.Increment to increment the counter in a thread-safe manner.
  • This way, using Interlocked.Increment, we ensure that even if multiple threads are accessing and modifying the variable counter, each increment operation will be atomic, meaning it will complete without interference from other threads. This is crucial in a multi-threaded environment to maintain data consistency and prevent race conditions.

Resource Loading Failures

  • Meaning and Impact of this bug: Resource loading failures occur when the game fails to load assets or resources, resulting in missing textures, models, or audio files.
  • How to Fix it: Verify file paths and ensure assets are correctly imported into the project.
  • Preventive Actions: Organize project assets properly, use Unity’s asset management system, and double-check file paths when loading resources.
  • Example: 
// Before:

AudioClip clip = Resources.Load<AudioClip>("SoundEffect"); // Error: Incorrect resource path

// After:

AudioClip clip = Resources.Load<AudioClip>("Audio/SoundEffect"); // Provide correct resource path

In the previous code, we correct the file path so that access to resources is granted.

Early Bird Tickets for the Irish Conference on Game-Based Learning are Available

The 13th Irish Conference on Game-Based Learning will be held online this year on 29th and 30th June 2023, and host over 25 presentations from international speakers on the benefits and uses of games for learning, motivation, and change.

Early Bird tickets are available for 1 week only here, and will give you access to teh full conference events, and some bonus content too.

Continue reading

Unity from Zero to Proficiency is Updated for Unity 2022

Hi there,

Just a quick update to let you know that the book Unity from Zero to Proficiency (Foundations) has been updated:

  • All screen-shots and explanations have been updated to be fully compatible with Unity 2022
    The Standard Asset project used has been updated to be full fully compatible with Unity 2022
  • For those of you who want to get started with 2D easily, the standard assets also include 2D assets (sprites, animations, etc) that you can use to move your 2D animated character.

You can get your free update by refreshing your Kindle reader using these steps:
https://www.amazon.com/gp/help/customer/display.html?nodeId=GBR7PXPE8JEJWM7U

If you have downloaded the book from my site (pdf) then just send me an email with your receipt, and full name, and I will send you the updated book ASAP.

 

That’s it

Chat soon

Pat

Standard Assets for Unity 2022+

If you have used Unity 2018 or previous versions of Unity, you may remember the standard assets included that made your life much easier when it came to prototype a new game; amongst other things, they included vehicles, character cameras, controllers, or water.

These are no longer available on the Asset Store, however, I have created a small project that includes all these assets, and that yo ucan use with Unity 2022+;

You can download this project here.

 

Enjoy!

January Sales: 50% Off on All books and video courses.

The January sale has started today, and with it you will be able to buy any or all of my books or courses with a 50% discount.

To get your 50% discount:

  • Use the coupon JANUARY2023SALE at the checkout for your pdf book (please see instructions below).
  • Use the coupon  2023JANSALES at the checkout for your video course  (please see instructions below).

I don’t do sales often, and I’m not sure when it will happen again, so do make sure that you avail of this opportunity today.

This sale is on for 48 hours; after that you will still be able to obtain my books, but this will be at the regular price.

countdownmail.com

How to Download Books

I have created a 50% discount coupon code that you can use on my website to download any book that may be relevant to you.

You can access all my books here.

The books that you can download cover a wide range of topics, to help you become better programmers and game developpers, including:

  • Java Programming,
  • JavaScript Programming,
  • C# Programming,
  • Unity from Zero to Proficiency (Five Books)

  • Godot from Zero to proficiency (Five Books)

  • Unreal Engine

  • Artificial Intelligence,
  • and much more.

To get your discounted books, you can do the following:

  • Choose the book that you would like to add to your cart by clicking on it to see more details on that book.

  • You then need to click on the link labeled “Download pdf from this site”.

  • You can then click on the button labeled “Add promo Code” and enter the code JANUARY2023SALE in the corresponding text field, and then press APPLY.

  • Once this is done, you should see that the discount has been applied.
  • You can then add as many books as you wish before checking out and you will be able to avail of a 50% discount on the cart total.

How to Access your Discounted Courses

You can access all my video courses here.

The courses cover a wide range of topics, to help you become better programmers and game developpers, including:

  • C# Programming.
  • Artificial Intelligence.
  • 3D Animation.
  • Unity.
  • Game Design.

 

To gain access to your discounted video course, you can do the following:

  • Choose (i.e., click on) the book that you would like from the list of courses.

  • Click on the button to “Enroll” the course.

  • In the new window, enter the discount code 2023JANSALES

  • The cart should now show the discount of 50%.

 

After you click on the “Buy Now button“, you should be able to start your course or to buy more courses discounted at 50% if you wish to do so.

 

 

 

countdownmail.com

The New Book on Python is Out Today!

 

This is just to let you know that I am releasing the second book in the series “Python Games from Zero to Proficiency” today.

This book includes six chapters that painlessly guide you through the necessary skills to master Python and Pygame, use its core features, and create 2D games with audio, and animated graphics.
After completing this book, you will:

  • Be comfortable with Python.
  • Use common structures to create programs in Python (e.g., loops, conditional statements, etc.).
  • Know and master the features that you need to create 2D games (user interface, collision and keyboard detection).
  • Create popular features found in pacman or shooter games.
  • Create and instantiate classes using Python.
  • Create and manage an inventory of weapons for the player character using classes and lists.
  • Create and manage weapons and ammunitions that the player character can collect and use.
  • Create Artificial Intelligence for NPCs so that they can see or hear the player.
  • Make it possible for NPCsC to patrol, detect and follow the player
  • Create a finite state machine to manage the behaviour of NPCs.
  • Learn how to use the Pygame library. .

You can get is book this week from for only $2.99 before the price goes up on Monday.

Here is the link to grab your copy:

Enjoy!

All the best

How to Access and Modify a Database From Unity


Accessing and Updating a Database

In this tutorial, we will learn how to interact with a database from Unity, using some simple but effective techniques.

After completing this chapter, you will be able to:

  • Understand basic database concepts.
  • Understand how online databases can be accessed through scripting.
  • Understand how to access a database from Unity.
  • Save and access information about a player.

Continue reading

How to Create an Infinite Runner or Platform Game in Unity

Creating and moving the main character

In this section, we will start by creating an infinite runner game, a classic game genre on mobile devices; this game will have the following features:

  • The player will have to avoid obstacles by jumping above them.
  • Jumps will be performed when the player presses a key (for the web versions) or when s/he taps once on the right side of the device’s screen (for the Android version).
  • The obstacles will come from the right side of the screen.
  • If the player hits one of the obstacles, the game will be restarted.
  • The player will have the option to pause the game.
  • The score will increase with time.

So, after completing this chapter, you will be able to:

  • Create a simple infinite runner.
  • Add simple controls to the main character.
  • Generate obstacles randomly.
  • Detect collisions.
  • Create a simple environment with basic shapes.

Figure 1: The final game

Continue reading

How to Create a Card Game in Unity

 

In this post, we will create a simple card guessing game where the player has to remember a set of 20 cards and to match these cards based on their value.

After completing this post, you will be able to:

  • Create a card game.
  • Change the sprite of an object at run-time.
  • Check when two cards picked by the player have the same value.
  • Shuffle the cards.

Continue reading