I am working on this script that instantiates objects in a certain marked area at a certain amount. For testing I made a corn field and didn't want any corn to get instantiated one in another so I created one more script which just check if two corn are too close they destroy each other. Now for the instantiating script which I will call the spawning script from now on, I made it so that at the start a method is called which spawns all the corn at random heights, rotations and of course positions. Now if some corn is too close to one another it is destroyed so then in the update method the spawn method is called again and it calculated how much more corn it needs to spawn, also when the spawn method finished it sets a boolean to true which can let the script call that method again from the Update method so that it doesn't flood or lead to any errors. My problem is that when it calls the spawn method from the Update method it doesn't work and there is no reason why it shouldn't.
Here is the spawn script:
using UnityEngine;
using System.Collections;
public class PrefabPainter : MonoBehaviour {
public int spawnedPrefabs;
public int amountOfPrefabs;
private int toSpawn;
private int prefabsSpawned;
public bool spawnedAll;
public float minimumHeight;
public float maximumHeight;
private float randomHeight;
private float randomPositionX;
private float randomPositionZ;
private float randomRotationY;
private Quaternion randomRotation;
public GameObject point1;
public GameObject point2;
public GameObject prefabToSpawn;
private GameObject currentSpawnedObject;
public GameObject[] prefabs;
void Start () {
spawnedAll = false;
prefabs = new GameObject[amountOfPrefabs];
SpawnPrefabs (amountOfPrefabs);
point1.GetComponent<MeshRenderer> ().enabled = false;
point2.GetComponent<MeshRenderer> ().enabled = false;
gameObject.GetComponent<MeshRenderer> ().enabled = false;
}
void Update () {
if (spawnedAll == true) {
if (spawnedPrefabs < prefabs.Length) {
toSpawn = prefabs.Length - spawnedPrefabs;
Debug.Log ("Needs to Spawn " + toSpawn + " Prefabs!");
SpawnPrefabs (toSpawn);
spawnedAll = false;
}
}
}
void SpawnPrefabs (int amountToSpawn) {
if (prefabsSpawned < amountToSpawn) {
for (int i = 0; i < amountToSpawn; i++) {
randomPositionX = Random.Range (point1.transform.position.x - 1, point2.transform.position.x);
randomPositionZ = Random.Range (point1.transform.position.z - 1, point2.transform.position.z);
randomRotationY = Random.Range (0, 360);
randomRotation = Quaternion.Euler (0, randomRotationY, 0);
randomHeight = Random.Range (minimumHeight, maximumHeight);
currentSpawnedObject = (GameObject)Instantiate (prefabToSpawn);
currentSpawnedObject.transform.rotation = randomRotation;
currentSpawnedObject.transform.position = new Vector3 (randomPositionX, transform.position.y + prefabToSpawn.transform.localScale.y, randomPositionZ);
currentSpawnedObject.transform.localScale = new Vector3 (currentSpawnedObject.transform.localScale.x, randomHeight, currentSpawnedObject.transform.localScale.z);
if (currentSpawnedObject.tag == "corn")
currentSpawnedObject.GetComponent<Corn> ().spawnedBy = gameObject;
for (int k = 0; k < prefabs.Length; k++) {
if (prefabs[k] == null) {
prefabs[k] = currentSpawnedObject;
break;
}
}
prefabsSpawned += 1;
spawnedPrefabs += 1;
Debug.Log ("Amount To Spawn is " + amountToSpawn + ", i is " + i);
if (i == amountToSpawn - 1) {
Debug.Log ("Spawned All");
spawnedAll = true;
}
}
}
}
}
and here is the Corn script:
using UnityEngine;
using System.Collections;
public class Corn : MonoBehaviour {
public GameObject spawnedBy;
void OnTriggerStay (Collider col) {
if (col.gameObject.tag == "corn") {
spawnedBy.GetComponent<PrefabPainter> ().spawnedPrefabs -= 1;
Destroy (this.gameObject);
}
}
}
Aucun commentaire:
Enregistrer un commentaire