Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Topthink · Dec 28, 2016 at 11:19 PM · objectlistrandomclassinstance

How do I Create 5 Random Racers?

I'm having a heckuva time figuring out how to create a persistent/permanent group objects that I can select from randomly to race each other over a preset course.

Right now, my racers are primitive spheres (NavMeshAgents). Right now, I can create five or ten spheres and assign them random speed and acceleration numbers and have them race. No problem there. I know how to do all that.

But, at some point, I want to create hundreds of objects and randomly select a few each time to race over one of my courses.

I know how to create the race course and the nav mesh and obstacles and bake them and all that...absolutely no problem with any of that stuff...I already have all that in place and it works fine. I can even manually create the racers and give them random speed/acceleration and have them race...that works fine too.

BUT...

I don't know how to create the List or Class or Group of racers. Once created, their speed and acceleration should remain the same...those should never change. The only thing that changes is that in one race, I might randomly select Racers 5, 17, and 22 to compete. In the next race, I might select 4, 13 and 26.

I've been looking up all sorts of stuff and I can't seem to figure out how to accomplish this.

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
Best Answer

Answer by ForbiddenSoul · Dec 29, 2016 at 09:09 AM

There are a whole bunch of ways to do this. You could for example have a scrip loop 100 times create a racer, and then add them to something like GameObject array, they will keep their initial properties, and you can just pick them by index number.

 // Total number of racers you want
     int numberOfRacers = 100;
     // An array to store them all in
     GameObject[] racers;
     void MakeRacers()
     {
         // This will loop through as many times as you have set "numberOfRacers" to.
         for (int i = 0; i < numberOfRacers; i++)
         {
             racers[i] = // the GameObject of a racer that your other code is making with the random values etc.
         }
     }
 
     int numberOfRacersToSelect = 12;
     GameObject racerToRace;
     void RandomSelection()
     {
         // This will loop through as many times as you have set "numberOfRacersToSelect" to.
         for (int i = 0; i < numberOfRacersToSelect; i++)
         {
             // Get a random float from 0 to the total number of racers you have, and have Mathf.RoundToInt make sure it
             // is a usuable integer for our array
             racerToRace = racers[Mathf.RoundToInt(Random.Range(0, numberOfRacers))];
             // Note that there is nothing in place to stop you from rolling the same random number multiple times.
         
             // CODE FOR WAHT TO DO WITH racerToRace GOES HERE
         }
     }


Something more efficient and flexible would be to use a C# indexers: https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

Edit: Indexers don't seem to be playing nice with Unity> I would use something else for now.

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 Topthink · Dec 29, 2016 at 05:58 PM 0
Share

Thank you too. I'm going to look at what you have immediately. I appreciate you taking the time to help me.

avatar image Topthink · Dec 29, 2016 at 06:04 PM 0
Share

Thanks for the heads up on selecting something twice. I've done this in C# outside of Unity...I'm just struggling with it inside the Unity Engine...I'm missing something basic and It's not clear what it is to me just yet. Outside Unity, I had put in an additional field to indicate if it was selected so it would not get selected twice. Once I figure out how to get all this into Unity, I don't think I'll have a problem selecting anything twice.

avatar image Topthink · Dec 30, 2016 at 10:59 PM 0
Share

I'm still looking at this.

avatar image Topthink · Dec 31, 2016 at 05:43 PM 0
Share

I'm still looking at this. I'm able to create the full universe of random racers...I think I'm doing fine at creating any number of these...and I list them up on a GUI to display them. No problem.

However, when I select three at random (or whatever number) I'm having great difficulty in figuring out how to assign one of the racers from the universe to my selection of current racers.

I've been trying a lot of ways to do this but I can't quite get it figured.

avatar image ForbiddenSoul · Dec 31, 2016 at 09:56 PM 0
Share

Sorry I took so long to reply, I have been busy over the holidays. Glad your making headway.

How is your universe setup? You say you can reference the racers in your GUI, can you not do the same or similar to reference them to your current racers?

avatar image ForbiddenSoul ForbiddenSoul · Jan 01, 2017 at 02:26 PM 0
Share

I don't know if this will help at this point, but I rewrote the code to account for duplicate random numbers and handled racer selection a little differently.

The code is too many characters to be a comment so here is a link: RandomRacers.cs

Show more comments
avatar image
0

Answer by allenallenallen · Dec 29, 2016 at 02:12 AM

How about creating a class with all the information stored as variables? And then have a list of that class saved. Whenever you want to select the racers, just recreate them again from the list.

Something like this?

 using UnityEngine;
 using System.Collections;
 
 public class Racer: MonoBehaviour // Probably don't even need MonoBehaviour depending on what you do
 {
     public float speed;
     public float acceleration;
 
     // This is where you are making a new car. 
     public Racer (float s, float a){
         speed = s;
         acceleration = a;
     }
 
 }

And somewhere else, you create a list to store the Racers.

 List<Racer> racers = new List<Racer>();
 racers.Add( new Racer(4.0f, 5.0f));  // Obviously, you should make a for loop and use random values

And later you can reassign the values to an actual race car GameObject.

 float mySpeed = racers[0].speed;
 float myAccel = racers[0].acceleration;
Comment
Add comment · Show 5 · 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 Topthink · Dec 29, 2016 at 05:57 PM 0
Share

Thanks. I'm going to look into your suggestion right now. I asked this question yesterday but I haven't got a chance to look at any answers yet.

avatar image Topthink · Dec 29, 2016 at 06:15 PM 0
Share

Wow. I'm really looking hard at that List idea. Very strong. Occasionally, I would like to eli$$anonymous$$ate some racers and add others. Your list suggestion might be a way to simplify that greatly. It will take me some time to dissect your response. Thanks for your help.

avatar image Topthink · Dec 29, 2016 at 07:39 PM 0
Share

$$anonymous$$y initial impression is that I would almost have to create this class and list outside monobehaviour in a "creation" script and then pull data from the class/list and insert it into my nav mesh agents (which are the racers) in a "selection" script that would inherit from monobehaviour.

...or something like that.

I don't know how to do it other than with two separate scripts like that but my thought is that should be workable. I'll be trying that today as I go along. It will take me some time as I'm working on other things too but I will post something up if it doesn't work.

avatar image Topthink · Dec 30, 2016 at 10:59 PM 0
Share

I'm still looking at this. Haven't forgotten to Accept an answer.

avatar image Topthink · Dec 31, 2016 at 05:47 PM 0
Share

I'm thinking I won't be able to get around inheriting from monobehaviour so I'll need to access these with "AddComponent" etc. But I'm having some difficulty (syntax, or something) in how to code it properly so I can use the racer from the universe pool in my current race.

I've been looking over the documentation for this and doing some searches, but I'm not having much luck as yet in figuring out how to write this.

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

69 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

Related Questions

Serialization - Variables won't change on original construction 1 Answer

Can I make a list of hashtables or classes with pragma strict 2 Answers

A node in a childnode? 1 Answer

How to store a class as a variable 1 Answer

Removing a class object from a list(.remove not working) 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