• 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
Question by CubePhysics · Jan 03, 2015 at 06:10 PM · instantiatearrayspawnrandom.range

How do i prevent an object instantiating another object straight away

my c# code below instantiates an array of gameobjects in a range of specific time. It all works fine though except I have a simple problem. In my game I have powerups and the spawner object that is instantiating the powerups instantiates it right where the spawner object is for the first time. so if my spawnMin was 7 and spawnMax was 20 it would rarely instantiate the object which it does but it also instantiates it once as soon as the script is called and it gets spawned right on the position of the object that is spawning it but only once? How do i prevent this?

     public GameObject[] obj;
     public float spawnMin = 1f;
     public float spawnMax = 2;
 
     void Start () {
         Spawn();
     }
 
     void Spawn()
     {
         Instantiate(obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
         Invoke("Spawn", Random.Range (spawnMin, spawnMax));
     }
Comment

People who like this

0 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 getyour411 · Jan 04, 2015 at 07:09 AM 0
Share

To the OP: Random.range (on int) is exclusive for MAX, so in your code example of min/max 1-2, 1 is the only possible result. Also look at InvokeRepeating.

avatar image CubePhysics · Jan 04, 2015 at 02:14 PM 0
Share

@HYPERSAVV Thanks appreciate it :D

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by HYPERSAVV · Jan 04, 2015 at 06:45 AM

A little confused by the wording of your question. What I got is that you have a spawner object, and I assume it is moving around in your game, and randomly you'd like it to spawn power-ups (which we want) but it also always spawns one right away (which we don't want).

Assuming that, your problem is that your first call to Spawn (in Start) will always be instantly when the script is enabled. Once you've called Spawn it's fine, because within Spawn we are Invoking Spawn, again, with a random time value. Another way to do this would be:

 void Start()
 {
    Invoke("Spawn", Random.Range( spawnMin, spawnMax));
 }
 
 void Spawn()
 {
    Instantiate(obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
    Invoke("Spawn", Random.Range (spawnMin, spawnMax));  
 }

tl;dr You need to treat the first call to Spawn like each successive call to it.

Comment

People who like this

0 Show 0 · 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

Answer by aaronpaulina · Jan 04, 2015 at 04:33 PM

You are calling Spawn() right away in start. That will call the function as soon as the object is loaded. Also by setting it's position to transform.position you are using the position of the spawner as your position for the powerup.

Change your Start method to something like this..:

 void Start()
 {
    InvokeRepeating("Spawn",2f,Random.Range(min,max));
 }

and your Spawn to :

 void Spawn()
 {
   GameObject newobj = (GameObject)Instantiate(obj[Random.Range(0,obj.Length]));
 
   newobj.transform.Position = new Vector3(Random.Range(minx,maxx),Random.Range(miny,maxy),Random.Range(minz,maxz));
 
 }

Comment

People who like this

0 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 HYPERSAVV · Jan 04, 2015 at 09:13 PM 0
Share

While InvokeRepeating would fix the first issue, although not entirely since the first invocation is a fixed time, it would introduce a new issue with lack of randomization. Each successive call to Spawn would happen at a constant interval.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Spawning Objects Using An Array. 1 Answer

Spawn from array 2 Answers

Instantiating prefab at child (spawnlocations are arrays) 2 Answers

onTrigger spawn object C# 3 Answers

add spawned GameObjects in an array C# 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