Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Ecnalyr · May 25, 2010 at 08:15 PM · destroyspawnmouseclickcounterleftmouseclick

Having a problem with maintaining a spawn of 10 objects on my screen.

I have two scripts trying to work together. One script spawns bubbles (BubbleSpawnerS), the other script destroys said bubbles when they are left clicked (DestroyOnLeftClick).

I want to have a maximum of 10 bubbles on the screen at any given time (see MaxBubbles variable), and this works great until I get to the point where I have to bubbles on the screen.

While the bubbles are initially spawning (one at a time at random time intervals), I can left click on them to destroy them and the game will keep spawning bubbles indefinitely until I hit a total of 10 bubbles (My bubble counter static variable TOTBUB goes up by 1 each time the BubbleSpawnerS script spawns a bubble and goes down by 1 each time DestroyOnLeftClick script destroys a bubble).

When I hit 10 bubbles for the first time though, the script stops spawning new bubbles even when I click a bubble and take the TOTBUB variable down to a number less than 10. (for some reason the game breaks down and does not behave like it does during the initial spawning of bubbles, before it hits the aximum amount of bubbles of 10.)

I need the game to spawn another bubble after I destroy one of the 10 bubbles on screen, and always do this up to the maximum of 10 bubbles. How do I accomplish this?

Here are the following scripts in their entirety:

BubbleSpawnerS static var TOTBUB = 0; var bubble : Transform; var MaxBubbles = 10; var MinTime = .1; var MaxTime = .8; var MinX = 0; var MaxX = 39; var MinY = 0; var MaxY = 14; function Start () { Spawn(); } function Spawn () { if (TOTBUB

var bubble = Instantiate(bubble, Vector3 (Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), 0), Quaternion.identity);//spawn a new bubble TOTBUB++; ReSpawn(); } } function ReSpawn() {

     if (TOTBUB!=MaxBubbles)
      {
       Spawn();
      }

 }

DestroyOnLeftClick

var buck : int = 0; function OnMouseOver () { if (Input.GetMouseButtonDown(0)) { buck+= transform.localScale.x; Destroy (gameObject); ScoreBoard.SCORE = ScoreBoard.SCORE + buck; BubbleSpawnerS.TOTBUB=BubbleSpawnerS.TOTBUB-1; } }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by duck · May 25, 2010 at 08:27 PM

One of your problems is that you're assigning the result of Instantiate to the 'bubble' variable, which means that 'bubble' is no longer pointing to your prefab, but on of your instances instead. If that instance gets destroyed, no more instances will be made (because the reference will be null).

Also, there's no need for a "Respawn" function. You just need your inital Spawn function to keep ticking over indefinitely, constantly checking whether it needs to make a new bubble.

It could look something like this instead:

static var TOTBUB = 0; var bubble : Transform; var MaxBubbles = 10; var MinTime = .1; var MaxTime = .8; var MinX = 0; var MaxX = 39; var MinY = 0; var MaxY = 14;

function Start () { while (true) { if (TOTBUB < MaxBubbles) { yield WaitForSeconds(Random.Range(MinTime, MaxTime)) var randomPos = Vector3 (Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), 0); Instantiate(bubble, randomPos, Quaternion.identity); TOTBUB++; } yield; } }

Comment
Add comment · Show 1 · Share
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 Ecnalyr · May 25, 2010 at 08:47 PM 0
Share

Thanks lots. Works like a charm.

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

No one has followed this question yet.

Related Questions

Level spawning statement help 0 Answers

List what spawner enemy spawned from? 1 Answer

Trigger...Spawn...Destroy 1 Answer

How can i destroy multiple objects with tag? 2 Answers

I can't get Destroy(collision.gameObject) to work? 2 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