Quantcast
Viewing all articles
Browse latest Browse all 1488

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(); } // 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)

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>