Question by
KAYUMIY · Jun 15, 2016 at 06:29 PM ·
instantiate prefab
Instatiante object's script is not working in run time
using UnityEngine;
public class EnemyManager : MonoBehaviour
{
public PlayerHealth playerHealth;
public GameObject enemy;
public float spawnTime = 3f;
public Transform[] spawnPoints;
public static bool once;
void Start ()
{
Invoke("Spawn", spawnTime);
}
void Update()
{
if (once)
{
Invoke("Spawn", spawnTime);
once = false;
}
}
void Spawn ()
{
if(playerHealth.currentHealth <= 0f)
{
return;
}
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
In runtime, first enemy is instantiated in Start function. For the first enemy, all scripts attached to enemy are working well. However, scripts attached to enemy which are instantiated in Update function are not working. If I remove "once" bool variable and if statement, scripts attached to enemy are working well. Problem is related to bool variable and if statement. Why?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612083012im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiated prefabs all stacking on top of each other 1 Answer
Instantiate and launch prefab that's overlapping player? 1 Answer
Rotation not working properly when ,Script rotation not working when instansiating. 0 Answers
How to load different content from database when select different option? 0 Answers