How to instantiate the whole object with script attached to it ?
Hi, so I am making a clicker game. There is a player. He is shooting an enemy. After the enemy is dead, I want to spawn another one. I figured out that I have to make a prefab of enemy. There is a script attached to my enemy - HealthBar. When I want to initialize the enemy with script it doesnt work, but when I delete the script, it is spawning. But I want to spawn the whole enemy with the script.
I really appreciate every idea!
There is my code for Initialization:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnEnemy : MonoBehaviour {
public GameObject Enemy;
GameObject EnemyClone;
public Transform Spawner;
// Use this for initialization
void SpawnEnemy () {
EnemyClone = Instantiate(Enemy, transform.position, transform.rotation) as GameObject;
EnemyClone.transform.SetParent(Spawner, false);
EnemyClone.GetComponent<EnemyHealth>();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("x"))
{
SpawnEnemy();
}
}
}
The Key "x" is just fort testing, I will change it later to (When first enemy is dead, spawn another one)
Your answer
Follow this Question
Related Questions
How can I change scene or Quit game after killing all the spawned enemies? 1 Answer
wave spawner difficulty increase 1 Answer
C# Photon Networking - Preventing duplicate GameObjects from spawning on Join. 1 Answer
Moving a spawned object in a direction 1 Answer
Game Manager can't decide what the instance of the object is despite just making an instance of it 1 Answer