Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spawn Question

Discussion in 'Scripting' started by simone9725, Feb 28, 2018.

  1. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Hi ,I have some strange issues with the spawn of my ball i don't know why when I collide with the ground insted o 1 ball 1000 of balls appear .What Can i do?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RespawnFromGround : MonoBehaviour {
    6.     public GameObject Ball;
    7.  
    8.  
    9.     private float minX = -2.65f, minY = -2.40f, maxX = 7.0f, maxY = 3.21f;//This position are the minimux and maximum value where the ball will be spawned
    10.     // Use this for initialization
    11.     IEnumerator Respawn(){
    12.         yield return new WaitForSeconds (2f);
    13.         GameObject Clone=Instantiate (Ball, new Vector3 (Random.Range (minX, maxX), Random.Range (minY, maxY), 0), Quaternion.identity)as GameObject;//We instatiate the object ball randomly
    14.  
    15.     }
    16.     void OnCollisionEnter2D(Collision2D target)
    17.     {
    18.         if (target.gameObject.tag == "Ball")
    19.         {
    20.             Debug.Log("collisone");
    21.             StartCoroutine (Respawn ());
    22.             Debug.Log("partira");
    23.         }
    24.     }
    25. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    When a new ball is spawned, it immediately collides with the floor, which spawns another ball. Which immediately collides with floor. Etc.

    One solution would be to keep track of the last ball that was spawned and when it was spawned. If the new collision is that same ball and it's been less than say 0.25 seconds, then don't spawn another one.

    Code (csharp):
    1. private GameObject lastSpawned;
    2. float timeLastSpawned;
    3. IEnumerator Respawn(){
    4.         yield return new WaitForSeconds (2f);
    5.         lastSpawned=Instantiate (Ball, new Vector3 (Random.Range (minX, maxX), Random.Range (minY, maxY), 0), Quaternion.identity)as GameObject;//We instatiate the object ball randomly
    6. timeLastSpawned = Time.time;
    7.     }
    8.  
    9. void OnCollisionEnter2D(Collision2D target)
    10.     {
    11.         if (target.gameObject.tag == "Ball" && !(target.gameObject == lastSpawned && Time.time < 0.25 + timeLastSpawned) )
    12.         {
    13. // etc
     
  3. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Hi and thanks for your reply i ve tried you suggestions but nothing has changed after the collision i have 6/7 ball clones :(
     
  4. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    That's probably due to the fact that onCollison is calling multiple times.
     
  5. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Ok man thanks so how can i avoid the collision go on?
     
  6. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Another idea comes to mind. If this new ball is going to be used to go up in the air and come back down, have a variable on it that's set to 'BeenUp = false' or something. Now, when it leaves the ground set it to true and only allow the respawn to happen if BeenUp is true.
    (This response is given with a little bias towards imaging the OP is shooting basket balls , based on a previous private conversation..)

    If you describe the game play situation a little bit, Simon, people might have a better understanding ;)
     
    simone9725 likes this.