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 /
avatar image
0
Question by El-Deiablo · Aug 20, 2016 at 03:26 AM · instantiatespawningdestroy objectinvokerepeatinglimitations

Invoke Ball Obstacle based on Score

In the beginning of my game there is one instantiated bouncing ball which works great. As the player earns points up to 3 balls may be present on the screen at one time. I have three separate ball spawners in my scene. One is activated right away and the others activate once a certain score is reached. My issue is that up to 5 balls appear on the screen. I have been working on my code for hours trying to figure out a way to destroy a ball if there are already three visible on the screen. I am getting closer, but am still struggling. I was hoping someone might have a suggestion.

Here's my code - Used for Getting the Ball Moving:

 using UnityEngine;
 using System.Collections;
 
 public class BallScriptLeft : MonoBehaviour {
     
     private Animator anim;
     private Rigidbody2D rb;
     public GameObject ball;
 
     void Start(){
 
         anim = GetComponent<Animator> ();
         rb = GetComponent<Rigidbody2D> ();
         rb.velocity = new Vector2 (5f, 5f);
 
     }
 
     void Update(){
 
         if (BallControllerLeft.bouncingBall == true) {
 
             StartCoroutine (DestroyBall ());
 
         }
 
         if (ScoreManager.scoreCount >= 25000 && ScoreManager.scoreCount <= 50000) {
 
             anim.speed = 1.2f;
 
         }
 
         if (ScoreManager.scoreCount >= 50000 && ScoreManager.scoreCount <=75000) {
 
             anim.speed = 1.5f;
 
         }
 
         if (ScoreManager.scoreCount >= 75000 && ScoreManager.scoreCount <=100000) {
 
             anim.speed = 1.8f;
 
         }
 
 
     }
 
     void OnTriggerEnter2D(Collider2D target){
 
 
     }
 
     IEnumerator DestroyBall(){
 
         yield return new WaitForSeconds (BallControllerLeft.bounceTime);
         Destroy(ball);
         BallControllerLeft.bouncingBall = false;
 
     }
 }

Code 2 - Used for Instatiating Ball:

 using UnityEngine;
 using System.Collections;
 
 public class BallControllerLeft : MonoBehaviour {
 
     public static int bounceTime;
 
     public int spawnWait;
 
     public Transform [] spawnPoints;
 
     public GameObject ball;
 
     private int ballCount = 0;
     private int level=0;
 
     public static int ballOnField=0;
 
     public static bool bouncingBall=false;
 
     void Start(){
 
     }
 
     void Update () {
 
 
         if (ballCount == 1||BallBoxCollider.isDestroyed == true) {
 
             StartCoroutine (OneBallAtATime ());
             ballCount = 0;
             BallBoxCollider.isDestroyed = false;
 
         }
 
         if (ScoreManager.scoreCount >= 25000 && ScoreManager.scoreCount <=50000 && level==0) {
             
             level++;
 
             bounceTime = 20;
             spawnWait = 5;
 
             InvokeRepeating("SpawnBall",spawnWait,spawnWait);
 
             if (BallBoxCollider.isDestroyed == true) {
 
                 StartCoroutine (OneBallAtATime ());
                 level = 0;
 
             }
 
             //Invoke ("SpawnBall", spawnWait);
             //StartCoroutine (OneBallAtATime ());
 
         }
                 
 
         if (ScoreManager.scoreCount >= 50000 && ScoreManager.scoreCount <=75000) {
 
             bounceTime = 30;
 
         }
 
         if (ScoreManager.scoreCount > 75000) {
 
             bounceTime = 40;
 
         }
 
     }
 
     void SpawnBall(){
 
         ballCount=1;
         ballOnField++;
         bouncingBall = true;
         Debug.Log ("SpawnBall Active");
         int spawnIndex = Random.Range (0, spawnPoints.Length);
         //int objectIndex = Random.Range (0, ball.Length);
         Instantiate (ball, spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation );
 
     }
 
     IEnumerator OneBallAtATime(){
 
         Debug.Log ("Left Ball Spawned");
         CancelInvoke ("SpawnBall");
         yield return new WaitForSeconds (bounceTime);
         Debug.Log ("Left Ball Spawning");
         ballOnField = 0;
         InvokeRepeating("SpawnBall",spawnWait,spawnWait);
 
     }
         
 }

Code 3 - Used for Destroying Ball:

 using UnityEngine;
 using System.Collections;
 
 public class BallBoxCollider : MonoBehaviour {
 
     public GameObject ball;
 
     public static bool isDestroyed=false;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
     
         if (BallController.ballOnField > 1||BallControllerLeft.ballOnField > 1||BallControllerTop.ballOnField > 1) {
 
             Destroy (ball);
 
         }
             
     }
 
     void OnTriggerEnter2D(Collider2D other){
 
         if (other.tag == "Player") {
 
             BallController.ballOnField = 0;
 
             isDestroyed = true;
 
             Destroy (ball);
 
             Debug.Log ("Collision Detected");
 
         }
 
     }
 }

Comment
Add comment · Show 1
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 Rajeesh_AR · Aug 20, 2016 at 06:33 AM 2
Share

Hi,

You could assign the ball to a gameobject while instantiating, so that you can easily destroy the exact ball.

eg:

     GameObject ballPrefab1;
     ballPrefab1 =   (GameObject)Instantiate (ball, spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation );


So, to destroy the first ball you could call Destroy(ballPrefab1).

If you have more balls, you could create more gameObjects such as ballPrefab1,ballPrefab2.... or you could create array or arrayList .. Since you are using maximum of 3 balls its good not to use array.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

61 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

Related Questions

how to destroy a object only in game and not the prefab 2 Answers

Prevent object from spawning at a spawn point twice in a row? 3 Answers

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 0 Answers

Following Object can't find newly Instantiated Object (Solved) 1 Answer

Respawning Single Enemy at a Random Range 1 Answer


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