About 'Making Scripts Add Components'!

In this video (objectives)…

  1. Remove colliders from asset pack prefabs, returning to vanilla state.
  2. Arrange for enemies to add their own colliders on start.

After watching (learning outcomes)…

Arrange for game objects to add their own required components at run time, reducing their dependency on assets being setup a particular way.

(Unique Video Reference: 27_AA_CU2)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

A good lecture that enabled me to customise a little.

I had an issue where i have rescaled my enemies as they were tiny and so the box colliders being added were based on the mesh render size. Not entirely helpful.
This meant i needed a script for each enemy that needed box colliders adjusting.
This also meant some of my enemies needed the collider center moving too.

I could sort this out playing with scale and sorting the enemy out but i chose to go code and do it at run time.
Note I am aware that i can do this both in the enemy script and only use one but for simplicity if you have multiple enemies i made them separate.
In the future this will get refactored into one script probably utilizing tags in unity as i have flying and ground units.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyWarrior : MonoBehaviour 
{

 //   [Tooltip("FX Prefab on Player")] [SerializeField] GameObject enemyDeathFX;

    // Use this for initialization
    void Start () 
	{
        AddNonTriggerBoxCollider();
	}

     void AddNonTriggerBoxCollider()
    {
        if (gameObject.AddComponent<BoxCollider>() == null)
        {
            Collider boxCollider = gameObject.AddComponent<BoxCollider>();
            boxCollider.isTrigger = false;
            BoxCollider b = GetComponent<BoxCollider>();
            if (b != null)
            {
                b.size = new Vector3(0.75f, 0.25f, 0.75f);
                b.center = new Vector3(0f, 0.25f, 0f);
            }
        }
        else
        {
            Collider boxCollider = GetComponent<BoxCollider>();
            boxCollider.isTrigger = false;
            BoxCollider b = GetComponent<BoxCollider>();
            if (b != null)
            {
                b.size = new Vector3(0.75f, 0.25f, 0.75f);
                b.center = new Vector3(0f, 0.25f, 0f);
            }
        }
    }

    // Update is called once per frame
    void Update () 
	{
		
	}

    void OnParticleCollision(GameObject other)
    {
        Destroy(gameObject);
    }
}```

I agree. If we are looking to not have to worry about updating the assets this lecture should also cover scale and rotation and probably check if there is already a collider

1 Like

Privacy & Terms