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 RoboticSarcasm · Nov 07, 2014 at 06:11 PM · c#random.range

Random number exclusion works only once [C#]

So for the game i'm making i want to spawn a score box randomly at any one of my preset spawn locations, however because i don't want the box to spawn at the same position as the previous one i have tried to exclude the last generated number. Sadly, this only works once, if it even works of all of which i'm not certain. Does anybody know how to fix this?

CODE: public class gameSystems : MonoBehaviour { public float lastNumberGenned; public float rdmFloat; public Transform boxToSpawn;

     public float score;
     public AudioClip pickupAudio;
     
 
     void Awake () {
 
     spawnBox();
 
     }
     
     
 
     void AddScore(float scoreAdded)
     {
         audio.PlayOneShot(pickupAudio);
         score += scoreAdded;
 
     }
 
 
     void genRandomNumber ()
     {
 
         rdmFloat = Random.Range(1, 10);
         spawnBox();
     }
 
     void spawnBox()
     {
         
         if (rdmFloat == lastNumberGenned)
         {
             genRandomNumber();
             return;
         }
         
             
             
                 if (rdmFloat != lastNumberGenned)
                 {
                     if (rdmFloat == 1) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn1").transform.position, Quaternion.identity); }
                     if (rdmFloat == 2) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn2").transform.position, Quaternion.identity); }
                     if (rdmFloat == 3) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn3").transform.position, Quaternion.identity); }
                     if (rdmFloat == 4) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn4").transform.position, Quaternion.identity); }
                     if (rdmFloat == 5) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn5").transform.position, Quaternion.identity); }
                     if (rdmFloat == 6) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn6").transform.position, Quaternion.identity); }
                     if (rdmFloat == 7) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn7").transform.position, Quaternion.identity); }
                     if (rdmFloat == 8) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn8").transform.position, Quaternion.identity); }
                     if (rdmFloat == 9) { Instantiate(boxToSpawn, GameObject.Find("boxSpawn9").transform.position, Quaternion.identity); }
                     lastNumberGenned = rdmFloat; 
 
                 }
                 
         
     }
 
 }
 
Comment
Add comment · Show 4
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 sysameca · Nov 07, 2014 at 06:28 PM 0
Share

As far as i can see your spawnBox() method executes only once inside the Awake() method. In order to randomly execute your method more than once you have to decide some kind of logic to drive it. Can you provide more details how you spawn your prefab in order to get an accurate result?

avatar image RoboticSarcasm · Nov 07, 2014 at 06:31 PM 0
Share

okay for some reason this excluded some code, i've edited it now (probably by the time you read this i have)

avatar image sysameca · Nov 07, 2014 at 06:34 PM 0
Share

There is not enough information to help you out. $$anonymous$$aybe you could write how and when exactly you want your prefab to spawn.

avatar image RoboticSarcasm · Nov 07, 2014 at 06:39 PM 0
Share

holy shit i am an idiot. Looking back at code i forgot that i NEVER called the function outside of Awake()... i thought i called it in another script..

2 Replies

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

Answer by RoboticSarcasm · Nov 07, 2014 at 06:41 PM

I am an idiot. Forgot to call the void from the script i wanted to call it...........

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
0

Answer by Itaros · Nov 07, 2014 at 06:13 PM

Change

 else genRandomNumber();


to

 else spawnBox();

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 RoboticSarcasm · Nov 07, 2014 at 06:17 PM 0
Share

sadly, does not work :(

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

28 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to slow down random enemy spawn? 1 Answer

I can't get Unity to recognise Random.Range C# 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in 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