Creating a custom behaviour

You can create your own MonoBehaviours, meaning you can do pretty much anything you want if you try. This tutorial will teach you how to create a simple behaviour and add it to your custom item (that you hopefully created in the previous tutorial).

1. Creating a behaviour class

Pretty simple really, especially if you know anything about Unity. You may read the Unity Documentation for this class if you want to know more. All you need to do is define a class that inherits MonoBehaviour inside the namespace you created. This can be in the script.cs file.

public class WhateverBehaviour : MonoBehaviour
{
    private void Start()
    {
        //Do whatever you want here
        //I guess we'll make it explode when spawned
        ExplosionCreator.CreateFragmentationExplosion(
            32, 
            transform.position, 
            4, 
            7, 
            true, 
            false, 
            0.5f);
    }    
}

2. Adding the component to your item

Also really easy to do. All that needs to be done is adding this line in your AfterSpawn function:

Instance.AddComponent<WhateverBehaviour>();

This will add the given component to the GameObject.

If everything went well, your script.cs file will look like this:

using UnityEngine; 

namespace Mod
{
    public class Mod
    {
        public static void Main()
        {
            ModAPI.Register(
                new Modification()
                {
                    OriginalItem = ModAPI.FindSpawnable("Brick"),
                    NameOverride = "Blue Brick lmfao -BlueBrick",
                    DescriptionOverride = "Like a brick but differently coloured.",
                    CategoryOverride = ModAPI.FindCategory("Misc."),
                    ThumbnailOverride = ModAPI.LoadSprite("blueBrickView.png"),
                    AfterSpawn = (Instance) =>
                    {
                        Instance.GetComponent<SpriteRenderer>().sprite = ModAPI.LoadSprite("blueBrick.png");
                        Instance.AddComponent<WhateverBehaviour>();
                    }
                }
            );
        }
    }

    public class WhateverBehaviour : MonoBehaviour
    {
        private void Start()
        {
            ExplosionCreator.CreateFragmentationExplosion(
                32, 
                transform.position, 
                4, 
                7, 
                true, 
                false, 
                0.5f);
        }    
    }
}

When you spawn the item you added, it should explode.

Attention!

This member is obsolete and should not be used. It is a remnant from the past.

bi bij bibi