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 meta_monkey · Jul 12, 2018 at 06:40 AM · scripting problemstrings

How to call a string inline as part of identifier of a new string?

This should be straight forward, but I can't seem to find an answer. I'm trying to declare a string (String 2 below) to be located in another script by using a different string (String 1 below) from the active script.

Working within clickToDestroy.cs and pulling string 2 from RigidBodyFirstPersonController.cs

String 1: currentCount = (1,2,3,4,5,...~) String 2: RigidbodyFirstPersonController.item(1,2,3,4,5,...~) (Depending on the value of currentCount, will be item1 item2 item3, etc.)

Is there a way to concatenate currentCount inline with RigidbodyFirstPersonController.item(insert currentCount here) during the declaration?

Comment
Add comment · Show 5
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 ShadyProductions · Jul 12, 2018 at 07:01 AM 0
Share

I really didn't understand anything of what you explained. What do you mean with 'concatenate currentCount inline with RigidbodyFirstPersonController.item(insert currentCount here)?

avatar image meta_monkey ShadyProductions · Jul 12, 2018 at 07:57 AM 0
Share

I'm sorry for the rough explanation.

If currentCount = 1, then I want to use something like RigidbodyFirstPersonController.item(currentCount) to represent RigidbodyFirstPersonController.item1.

avatar image YoloJero · Jul 12, 2018 at 07:28 AM 0
Share

Could you explain a bit better, what you are trying to archive? Currently I don't really understand your question. Please use the code formatting and provide a real code example of what you are trying.

avatar image meta_monkey YoloJero · Jul 12, 2018 at 07:59 AM 0
Share

I don't know the real code formatting.

If currentCount = 1, then I want to use something like RigidbodyFirstPersonController.item(currentCount) to represent RigidbodyFirstPersonController.item1. Do you know the syntax to add the value of currentCount into the name of the string that's being called?

avatar image YoloJero meta_monkey · Jul 12, 2018 at 08:45 AM 0
Share

The problem with your way of writing it with brackets and completely without quotation marks like

 RigidbodyFirstPersonControler.item(x,y,z,...~)


is that we asume RigidbodyFirstPersonControler.item is a method which you call with the parameters x,y, z, etc. But apparently that's not what you are trying, right?


Without you showing us some code it is very hard to understand/help you. $$anonymous$$y first guess: It is simply not possible. You can make a dictionary that maps the currentCount to an object like e.g.

 Dictionary<string, object> myDict = new Dictionary<string, object>(){
     ("1", RigidbodyFirstPersonControler.item1),
     ("2", RigidbodyFirstPersonControler.item2),
     ....
 }


or you also can create a dictionary within rigidbodyFirstPersonController (is this your script? .. then where is the code example of it?)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by YoloJero · Jul 12, 2018 at 08:53 AM

This is just a wild guess since I still don't get the explenation but maybe you mean something like string.Format ?

 string finalString = string.Format("{0} {1}", "Hello", "World!");


This will give you Hello World! as value for finalString.


so in your case

 string String2 = string.Format("RigidbodyFirstPersonController.item{0}", currentCount);


(assuming currentCount is a string and not an int or something, otherwise you could use currentCount.ToString())

Comment
Add comment · Show 3 · 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 meta_monkey · Jul 12, 2018 at 07:10 PM 0
Share

Thank you for the response. This is similar, but considering the above example I need to actually name the String2 using the string.

Rather than:

string String2 = string.Format("RigidbodyFirstPersonController.item{0}", currentCount);

I need something like:

string RigidbodyFirstPersonController.String{0} = (arbitrary string) with the "0" being currentCount

avatar image ShadyProductions meta_monkey · Jul 12, 2018 at 08:32 PM 0
Share

Why don't you just use a list, ins$$anonymous$$d of manually making all these strings.. If not, u should look into System.Reflection. It has what you want.

avatar image YoloJero · Jul 16, 2018 at 04:12 PM 0
Share

Please also refer the comment I made the same day on your question...

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

153 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 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 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

Localization via script works in editor but not in build, why? 1 Answer

Remove String Line If Contains Specific Word C# 1 Answer

Keeping the int value "min" to be used in another scene. 2 Answers

Make an object a certain distance from me. 2 Answers

Crouching help needed 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