Hi,
So I made a function that initialy spawns an ammo pack prefab after an interval.
using UnityEngine;
using System.Collections;
/// <summary>
/// SG
/// You can add item prefabs and spawnlocations, set a delay between spawn and the place / item is random
/// </summary>
public class PickupSpawner : MonoBehaviour
{
public static PickupSpawner _ps;
public float pickupDeliveryTimeMin = 10f; // Delay on delivery.
public float pickupDeliveryTimeMax = 30f;
public Transform[] spawnPoints; //were do we spawn the items?
public Transform[] ItemPrefabs; //what items can we spawn?
void Start ()
{
if (_ps == null)
{
_ps = GameObject.FindGameObjectWithTag("GM").GetComponent<PickupSpawner>();
}
// Start the first delivery.
StartCoroutine(InstatiatePickup(false));
if (spawnPoints.Length == 0)
{
Debug.LogError("No spawn points referenced.");
}
}
void SpawnUpgrade()
{
Transform _upgrade = ItemPrefabs[Random.Range(0, ItemPrefabs.Length)];
Debug.Log("Spawning Upgrade: " + _upgrade.name);
Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(_upgrade, _sp.position, _sp.rotation);
}
public IEnumerator InstatiatePickup(bool randomDelay)
{
// Wait for the delivery delay.
float randomPickupDeliveryTime = Random.Range(pickupDeliveryTimeMin, pickupDeliveryTimeMax);
Debug.Log("Waiting for " + randomPickupDeliveryTime + " seconds.");
yield return new WaitForSeconds(randomPickupDeliveryTime);
SpawnUpgrade();
}
}
So initially it is spawned by the script itself, the second time I call it when i trigger (pickup) the ammo pack.
using UnityEngine;
using System.Collections;
/// <summary>
/// SG
/// This code will determine if the object it is attached to has triggered a overlap with a collider object.
/// </summary>
public class PickUpTrigger : MonoBehaviour
{
public static PickUpTrigger PUT;
void Start()
{
PUT = gameObject.GetComponent<PickUpTrigger>(); ;
}
void OnTriggerEnter2D(Collider2D col)
{
//Debug.Log(gameObject.name + " has collided with " + col.gameObject.name);
if (col.gameObject.tag == ("Player1") || col.gameObject.tag == ("Player2") || col.gameObject.tag == ("Player3") || col.gameObject.tag == ("Player4"))
{
col.gameObject.GetComponentInChildren<FireBall>().GainItem(gameObject.name);
destroyItem(gameObject, 0, true);
Debug.Log("We should be giving the command to make new delivery of bullets!");
StartCoroutine( PickupSpawner._ps.InstatiatePickup(true));
}
}
public static void destroyItem(GameObject objectInNeedOfDestruction, float destroyTimer, bool hit)
{
Destroy(objectInNeedOfDestruction, destroyTimer);
}
}
The weird thing is that it does execute a part of the code a second time, if you see the log:
But I can wait as long as I want, it doesn’t spawn.
I tried to add the script to this gameobject aswel to make an instant reference, tried to declare it some other way. Nothing seems to work.
Thanks for the help!