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 ashjack · Mar 18, 2014 at 07:10 PM · c#vector3randombooleanrandom.range

Random spawn script not working correctly

What is wrong with this script?

I have a C# script(for and android game) that should spawn an object at a random position on the x axis between -50 and 50 ONCE, but it does neither at the minute. I get a constant stream of coins spawning in, when I just want one, and also, they are all in the same location. I don't get any errors, and cannot work out what is wrong with my script.

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour {
     
     public bool spawn;
     public Transform coin;
     public Vector3 pos;
     
     void Start()
     {
         pos = new Vector3(Random.Range(-50.0f, 50.0f), 50, 0);
         spawn = true;
     }
 
     void Update()
     {
         if(spawn = true)
         {
             GameObject instance = Instantiate(coin, pos, transform.rotation) as GameObject;
             spawn = false;
         }
     }
 }
 
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

3 Replies

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

Answer by Nanobrain · Mar 18, 2014 at 07:49 PM

Firstly, you are setting spawn to TRUE each update, and thus the IF statement is always evaluating to TRUE. Change '=' to '=='.

 if(spawn = true) // WRONG
 if(spawn == true) // CORRECT

Secondly, after making this change you will notice that only one object will ever spawn, and that's it. Somewhere in your code you need to change 'spawn' to == TRUE. So, let's add a simple timer.

 // add these members to your class
 float theTime;
 int spawnDurationSeconds = 3; // number of seconds between each spawn


 // In your Start method let's set the time to NOW
 theTime = Time.time;


 //In your Update method, place this above your 'IF spawn == true' statement
 if(Time.time - theTime > spawnDurationSeconds * 1000) {
     spawn = true;
     theTime = Time.time; // reset the time
     pos = new Vector3(Random.Range(-50.0f, 50.0f), 50, 0); // randomize the position
 }
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
avatar image
1

Answer by pako · Mar 18, 2014 at 07:37 PM

You get constant stream of coins spawning because you use Instantiate inside Update(). So Instantiate gets called in every frame. You must use Instantiate in a separate method, and call it from Update when, for example, a key is pressed (in the code below I use the space key to spawn). Also, the Random function must be called just before Instantiate, and not just once in Start().

e.g.

     using UnityEngine;
     using System.Collections;
      
     public class Spawner : MonoBehaviour {
      
     public Transform coin;
     public Vector3 pos;
      
     void Start()
     {
 
     }
      
     void Update()
     {
      if (Input.GetKeyDown(KeyCode.Space))
         {
          Spawn()
         }
     }
 
 
 
 
 void Spawn()
 {
 pos = new Vector3(Random.Range(-50.0f, 50.0f), 50, 0);
 
 Instantiate(coin, pos, transform.rotation) as GameObject;
 }
 
 }
 
Comment
Add comment · Show 4 · 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 Nanobrain · Mar 18, 2014 at 07:48 PM 1
Share

Actually, he is setting 'spawn' to TRUE in Update() by using the assignment operator '=' in his IF statement. He is not using the comparison operator '=='. Thus, his IF statement is evaluating to TRUE every loop.

avatar image pako · Mar 18, 2014 at 07:58 PM 0
Share

Nice catch, you're right. I always miss that!

avatar image Nanobrain · Mar 18, 2014 at 07:59 PM 0
Share

I got lucky :)

avatar image pako · Mar 18, 2014 at 08:05 PM 0
Share

Just edited my answer by removing the spawn variable. It's not necessary in this code i.e. if he wants to call spawn by pressing a key or from another script.

avatar image
0

Answer by ashjack · Mar 18, 2014 at 09:04 PM

Thank you for all your answers. I can't believe that I missed two obvious mistakes. I have turned off my computer now, and i'll post again if it doesn't work. But I'm sure that won't be necessary. Thanks again!

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

My Script Wont work? 2 Answers

Is Random.Range() Really Maximally Inclusive? 4 Answers

How to make Random.Range() step by a certain amount? 1 Answer

How can I do random choices 1 Answer

Distribute terrain in zones 3 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