- Home /
The question is answered, right answer was accepted
Problem with Coroutine or Istantiate
I have this code here that should spawn some pickups, exactly one per time and no more, but some times it randomly spawn two pickups at the same time and at the same position (i guess, but i'm not sure because the Unity editor says that there is only one pickup), i just know that when i pick it up sometimes (apparently random) i have 2 times the perk that normally i should get ( I have 2 bombs instead of 1, or I get double health that what i should get). I tried to understand the reason of this for almost a week but i'm stupid and i really don't know what is wrong.
I'll leave the code here:
using UnityEngine;
using System.Collections;
public class PickupSpawner : MonoBehaviour
{
public GameObject[] pickups; // Array of pickup prefabs with the bomb pickup first and health second.
public float pickupDeliveryTime = 5f; // Delay on delivery.
public float dropRangeLeft; // Smallest value of x in world coordinates the delivery can happen at.
public float dropRangeRight; // Largest value of x in world coordinates the delivery can happen at.
public float highHealthThreshold = 75f; // The health of the player, above which only bomb crates will be delivered.
public float lowHealthThreshold = 25f; // The health of the player, below which only health crates will be delivered.
void Start ()
{
StartCoroutine(DeliverPickup() );
}
public IEnumerator DeliverPickup()
{
// Wait for the delivery delay.
yield return new WaitForSeconds(pickupDeliveryTime);
// Create a random x coordinate for the delivery in the drop range.
float dropPosX = Random.Range(dropRangeLeft, dropRangeRight);
// Create a position with the random x coordinate.
Vector3 dropPos = new Vector3(dropPosX, 15f, 1f);
//get a random pickup from the 2 Existing
int pickupIndex = Random.Range(0, pickups.Length);
Instantiate (pickups[pickupIndex], dropPos, Quaternion.identity);
}
}
Hi, I think this script should work well. If also Unity Editor says there is only one object, it could be that there is an error in your pickup script that takes the object, but doesn’t destroy it for some reasons.
The code is very similar to this tutorial (i'm following it): https://assetstore.unity.com/packages/essentials/tutorial-projects/2d-platformer-11228
Since the problem is in both I tough the problem was in this script, but i too also noticed that it seemed fine...
the pickup script of the bomb is this (the health one is very similar and the problem is present in both) :
public class BombPickup : $$anonymous$$onoBehaviour
{
public AudioClip pickupClip; // Sound for when the bomb crate is picked up.
private Animator anim; // Reference to the animator component.
private bool landed = false; // Whether or not the crate has landed yet.
PickupSpawner pickupSpawner;
void Awake()
{
// Setting up the reference.
anim = transform.root.GetComponent<Animator>();
pickupSpawner = GameObject.Find("pickup$$anonymous$$anager").GetComponent<PickupSpawner>();
}
void OnTriggerEnter2D (Collider2D other)
{
// If the player enters the trigger zone...
if(other.tag == "Player")
{
// ... play the pickup sound effect.
AudioSource.PlayClipAtPoint(pickupClip, transform.position);
// Increase the number of bombs the player has.
other.GetComponent<LayBombs>().bombCount++;
// Destroy the crate.
Destroy(transform.root.gameObject);
//call in for another crate
pickupSpawner.StartCoroutine(pickupSpawner.DeliverPickup());
}
// Otherwise if the crate lands on the ground...
else if(other.tag == "ground" && !landed)
{
// ... set the animator trigger parameter Land.
anim.SetTrigger("Land");
transform.parent = null;
gameObject.AddComponent<Rigidbody2D>();
landed = true;
}
}
}
Now it works fine, Thanks a lot!!
You really helped me thanks