- Home /
Check if bool is true then run script?
How can i make it spawn a object when the bool state is true? i have it set up but it is not working why? this is C# script.
using UnityEngine;
using System.Collections;
public class cointopon : MonoBehaviour
{
public Transform[] spawnPoints;
public GameObject[] enemyPrefabs;
public float amountEnemies = 20; // Total number of enemies to spawn.
public float yieldTimeMin = 0;
public float yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.
public int i;
public bool state;
void Update() {
yieldTimeMin = PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolmin").Value;
yieldTimeMax = PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolmax").Value;
amountEnemies = PlayMakerGlobals.Instance.Variables.GetFsmFloat("coinboolamount").Value;
state= PlayMakerGlobals.Instance.Variables.GetFsmBool("coinbool").Value;
}
public IEnumerator Spawnblock()
{
if (state == true) {
for (i=0; i<amountEnemies; i++){
var randNum = Random.Range(0, spawnPoints.Length);
yield return new WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated.
var obj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)]; // Randomize the different enemies to instantiate.
var pos = spawnPoints[randNum];
Instantiate(obj, pos.position, pos.rotation);
}
}
}
}
Answer by codecranker · Apr 16, 2013 at 01:53 AM
First, some heads up with the code. Random.Range() returns a random within a specified range, inclusive. You might receive IndexOutOfRange exception.
Secondly, can you see your newly instantiated object in the hierarchy. It might be possible that it is off camera or behind something (depth wise) or not rotated correctly for the camera to view it?
Or are you getting any errors in the console during Instantiate()?
I have a counter that is what i is it counts the spawned objects but it shows 0 and i know the counter works.
public IEnumerator Spawnblock() would this be running ever frame if i am using send message?
I also know that the bool is true and then goes false when it is to.