• 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 /
  • Help Room /
avatar image
0
Question by grayfox489 · Jan 13, 2017 at 09:23 PM · spawnobjectsrangerandomly

Hey guys so I'm new to C# and our professor just assigned us homework, and I'm trying to figure out how to get the object to drop randomly within a certain range but I don't know so can someone help me he gave us this code:

using UnityEngine; using System.Collections;

public class SpawnerScript : MonoBehaviour {

 public GameObject ball1Prefab;

 private float spawnTimer;
 private float spawnCounter;



 void Start()
 {
     
     spawnTimer = 1.0f;
     spawnCounter = 0.0f;
 }
  
 void Update () {
     
     spawnCounter += Time.deltaTime * 1;

     if (spawnCounter >= spawnTimer) {
     
         SpawnEgg ();
         spawnCounter = 0;
     }
         


 }

 void SpawnEgg()
 {

     Instantiate (ball1Prefab, transform.position, Quaternion.identity);



 }

}

Comment
Add comment · Show 2
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 KoenigX3 · Jan 13, 2017 at 09:39 PM 0
Share

Could you specify your problem?

You mean drop within a certain range of position? And if so, what kind of range? Should we give a certain amount of range on all axes so it can be placed anywhere within a rectangle, or within a circle?

avatar image grayfox489 KoenigX3 · Jan 13, 2017 at 10:14 PM 0
Share

Sorry, what I mean by a certain range is like...ok so the code is for a invisible spawner that right above the game screen, and this is a left to right moving game in a 3D environment. So I guess the best I can explain it is, I want that spawner to spawn objects randomly on the X axis without going out of the game screen...I don't know if that helped but I'm kinda new at all this so sorry.

1 Reply

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

Answer by WTler · Jun 20, 2017 at 10:56 PM

Probably much to late, but it would have been something like this:

 void SpawnEgg()
 {
 float Min = -5f, Max = 5f;
  
          Vector2 newPos = new Vector2(Random.Range(Min, Max), 10);
          Instantiate(ball1Prefab, transform.position + newPos, Quaternion.identity);
 }


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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

87 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

Related Questions

How to paint on objects around an object? 1 Answer

Spawn objects from picture isnt working 1 Answer

How do I spawn a List of Objects, so that there is a delay after each object that spawned? 0 Answers

How do I spawn a List of Objects so that there is a delay after each object that spawned? 0 Answers

How to change Button input to Keyboard input? 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