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
0
Question by Megaz97 · Jun 18, 2014 at 01:59 PM · c#

I want there to only be 5 objects on screen at any time, can someone help please?

I am making an asteroids like game and I made 200 rocks respawn, however, I want there to only be 5 of the 200 rocks respawned on screen at any time. Can someone help please?

Here is my code.

     using UnityEngine; 
     using System.Collections;
     public class GameManager : MonoBehaviour {
     public GameObject[] RockPrefab;
     public int numRocks = 200;
     public GameObject rock;
     public Vector3 pos;
     public GameObject RocketPrefab;
     
         // Use this for initialization
         void Start ()
         {
          
          
          
         for (int i = 0; i < numRocks; i++) {
         pos = new Vector3 (Random.Range (-6F, 6F), 0, Random.Range (-6F, 6F));
         if (pos.x < 4 && pos.x > -4 && pos.y > -4 && pos.y < 4) {
         i--;
          
         } else
         Rock (pos, 1.0f);
         }
          
         }
          
          
          
         // Update is called once per frame
         void Update ()
         {
          
         }
          
         public void Rock (Vector3 RockPos, float rockSize)
         {
         rock = Instantiate (RockPrefab [Random.Range (0, 4)], RockPos, Quaternion.identity) as GameObject;
         rock.transform.localScale = new Vector3 (rockSize, rockSize, 0f);
          
         }
 }





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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Jun 18, 2014 at 02:10 PM

This logic is weird and almost certainly wrong:

 pos = new Vector3 (Random.Range (-6F, 6F), 0, Random.Range (-6F, 6F));
 if (pos.x < 4 && pos.x > -4 && pos.y > -4 && pos.y < 4) {
 i--;
  
 } else
 Rock (pos, 1.0f);

"Generate a Vector3 with x and z components between -6 and 6 and a y component of 0. If the x component is between -4 and 4 (and the y component is also between -4 and 4, which it always will be) then don't increment the loop counter, otherwise create a Rock"

Is that really what you wanted? As for why you're getting 200, what is the value of numRocks set in the inspector?

Comment
Add comment · Show 7 · 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 Rob-Fireproof · Jun 18, 2014 at 02:14 PM 0
Share

I interpreted that as trying to prevent them from spawning in the middle of the screen. Odd way to write it (I'd have a counter of rocks created, and "while (rocksCreated < numRocks)" loop personally, but I don't think it's wrong.

avatar image Megaz97 · Jun 18, 2014 at 02:16 PM 0
Share

Yes my apologies, I changed the numRocks to 5 for testing. It should actually be 200. I agree, the logic is a bit weird, but what it does is it stops the rocks from respawning at the player's position.

avatar image tanoshimi · Jun 18, 2014 at 02:26 PM 0
Share

@Rob-Fireproof - but pos.y is initialised at 0, and then immediately tested to see if pos.y > -4 && pos.y < 4.... ;)

avatar image Rob-Fireproof · Jun 18, 2014 at 02:31 PM 0
Share

@tanoshimi - Oh yeah! I see what you mean. I'm guessing those should be z's... :-)

avatar image Megaz97 · Jun 19, 2014 at 10:10 AM 0
Share

"I interpreted that as trying to prevent them from spawning in the middle of the screen. Odd way to write it (I'd have a counter of rocks created, and "while (rocksCreated < numRocks)" loop personally, but I don't think it's wrong."

@Rob-Fireproof Do you think you could show me how? Though my code works somewhat correctly, it seems to glitch every now and then, where the rocks will sometimes go out of range.

Show more comments
avatar image
0

Answer by Rob-Fireproof · Jun 18, 2014 at 02:06 PM

That looks like it should only spawn 5 to me. Is the numRocks variable on the actual GameManager component in the scene still set to 5?

Comment
Add comment · Show 3 · 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 Megaz97 · Jun 18, 2014 at 02:13 PM 0
Share

Yes my apologies, I changed the numRocks to 5 for testing. It should actually be 200.

avatar image tanoshimi · Jun 18, 2014 at 02:28 PM 0
Share

Hang on, so you want 200 and you're only getting 5, even after assigning the numRocks variable in the inspector to 200?

avatar image Megaz97 · Jun 19, 2014 at 09:52 AM 0
Share

@tanoshimi No, I have set the numRocks value to 200. Now although there are 200 rocks, I want only 5 of the 200 rocks to appear on camera at any time. Do you think something like that is possible?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple Cars not working 1 Answer

WaitForSeconds/Yield problem 1 Answer

C# -- Build character unit from script 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