Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 10, 2018 at 09:03 AM by Fix_a_Fix for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Fix_a_Fix · Jun 08, 2018 at 06:10 PM · instantiatecoroutinepickupspawning problemsspawner

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);
                     
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MT369MT · Jun 08, 2018 at 07:00 PM 1
Share

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.

avatar image Fix_a_Fix MT369MT · Jun 10, 2018 at 07:52 AM 0
Share

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;        
         }
     }
 }
avatar image Fix_a_Fix MT369MT · Jun 10, 2018 at 09:21 AM 0
Share

Now it works fine, Thanks a lot!!

You really helped me thanks

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having multiple objects fire prefabs in different times C# 0 Answers

Coroutines insider another coroutine. 1 Answer

Using event system to respawn dead enemies will spawn live ones again too 0 Answers

instantiating at a random time 1 Answer

Pickup & Drop Objects + Spawn items HELP 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges