- Home /
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?
I really didn't understand anything of what you explained. What do you mean with 'concatenate currentCount inline with RigidbodyFirstPersonController.item(insert currentCount here)?
I'm sorry for the rough explanation.
If currentCount = 1, then I want to use something like RigidbodyFirstPersonController.item(currentCount) to represent RigidbodyFirstPersonController.item1.
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.
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?
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?)
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()
)
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
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.
Please also refer the comment I made the same day on your question...