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 /
  • Help Room /
avatar image
1
Question by NinjaRubberBand · Jun 18, 2016 at 02:57 PM · scripting problemrandomcube

Convert Int to String

I have a lot of cubes in my scene, which all of them is named a number.

 #pragma strict
 
 
 var amount = 25;
 var objectNumber = 0;
 var object : GameObject;
 
 function Start () {
 
 objectNumber = Random.Range(1, amount);
 object = gameObject.Find(objectNumber);
 }
 

I want to randomly select one of the cubes by using random.range, and then select the choosen number, which one of the cubes has and put it in the object variable. The problem is, that the name of the cubes are strings, and i apparantly can't choose them with a int. Is there a way i can do this? I don't want to put each of them in a array. To much time since i will be doing it with a lot of cubes.

Comment
Add comment · Show 1
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 jgodfrey · Jun 21, 2016 at 01:23 AM 0
Share

I assume this question is answered. If that's the case, please accept the answer to close the loop on the question.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by jgodfrey · Jun 18, 2016 at 03:10 PM

There are lots of ways, including...

 int i = 10;
 string s;
 s = i.ToString();
 s = Convert.ToString(i);
 s = string.Format("{0}", i);
 s = "" + i;
 s = string.Empty + i;
 s = new StringBuilder().Append(i).ToString();

Either of the first two are probably the best / most typical. The remaining ones are just to show some variety...

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 Eno-Khaon · Jun 19, 2016 at 02:14 AM 0
Share

As an additional note, details on numeric string formatting can be found at their $$anonymous$$SDN entry. @jgodfrey­'s fourth example, s = "" + i; could be utilized in conjunction with the first, s = i.ToString(); to add further formatting and organization where necessary. For example, if all your cube objects include 3-digit numbers in their names (e.g. Cube002):

 // JS
 objectNumber = Random.Range(1, amount);
 var objectName: String = "Cube" + objectNumber.ToString("D3");
 object = gameObject.Find(objectName);

The random number is generated, then turned into a 3-digit value to append to any applicable prefix.

Additionally, bear in $$anonymous$$d that Random.Range is inclusive for the $$anonymous$$imum and exclusive for the maximum values. This means that when you define amount as 25, your random number will be between 1 and 24, never able to be 25.

avatar image
0

Answer by rhbrr5hrfgdfgw · Jun 18, 2016 at 11:41 PM

@NinjaRubberBand Yea sure, you can do something like:

      #pragma strict
      
      var amount = 25;
      var objectNumber = 0;
      var object : GameObject;
      
      function Start () {
      
      objectNumber = Random.Range(1, amount);
      object = GameObject.Find(objectNumber.ToString());
      }
 
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

57 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

Related Questions

Is it simple or is it complex? 2 Answers

Instantiate Random Prefab From Folder 1 Answer

So I want to rotate this cube... 1 Answer

Noob Here... Can you please give Script for moving a object into random Direction. 0 Answers

How to Play random animation for Attack 5 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