About 'Instantiating At Runtime'!

In this video (objectives)…

  1. Discuss how to recreate our explosion prefab.
  2. Why we use prefabs differently for the enemies.
  3. Instantiate an explosion on enemy death.
  4. Make the explosion FX self-destruct.

After watching (learning outcomes)…

Instantiate game objects at run time, parent them to in the hierarchy, and chose their position in world space.

(Unique Video Reference: 28_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!

Well i did as i promised and had a “Ben” moment of i’ll refactor it later and then No lets do it now.

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

public class Enemy : MonoBehaviour 
{

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

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

    void AddNonTriggerBoxCollider()
    {
        AddColliderWarrior();
        AddColliderDrone();
    }

    private void AddColliderDrone()
    {
        if (gameObject.tag == "PA_Drone")
        {
            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);
                }
            }
            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);
                }
            }
        }
    }

    private void AddColliderWarrior()
    {
        if (gameObject.tag == "PA_Warrior")
        {
            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);
                }
            }
        }
    }

    void OnParticleCollision(GameObject other)
    {
        GameObject fx = Instantiate(enemyDeathFX, transform.position, Quaternion.identity);
        fx.transform.parent = parent;
        Destroy(gameObject);
    }
}

Looking good Marc - I like the speed of your game :slight_smile:

1 Like

Just a heads up, at 2:05, Ben ticks the box next to the game object name. I missed it and it took me a while to figure out why the explosions weren’t showing when an enemy died :stuck_out_tongue_winking_eye:

5 Likes

Greetings All,

I’ve been working on my rail shooter and am liking so far what I see. LOTS more to add and do, but I’m starting to get that feeling of something that’s coming together. As always I would appreciate any feedback or comments. Thanks in advance and have a wonderful day. :slight_smile:

Jenn

5 Likes

looking Good, I really like the terrain you’ve done

1 Like

Ha…I did the same thing…glad you posted it here Victor

:+1:

Oh my gosh I had the same problem! Thank you for sharing this.

1 Like

Does the sun actually get brighter over time on the splash screen? That is so cool. It makes it feel very realistic.

Thank you! Came here just for this!

1 Like

Here is my clip for instantiating at runtime

edit: My Death effects code

using UnityEngine;

namespace Control
{
    public class EnemyController : MonoBehaviour
    {

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

        private void SpawnDeathEffects()
        {
            GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
            fx.transform.parent = parent;
            fx.AddComponent<Core.Cleaner>();
        }
    }
}

namespace Core
{
    public class Cleaner : MonoBehaviour
    {
        private void Start()
        {
            Destroy(gameObject, 5f);
        }
    } 
}

Privacy & Terms