Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm trying to reference an instance or object of a class in another script (using Unity). My problem is, when I execute the code, I want to change which object I am referencing based on a string variable that changes.

for example:

string y = otherScript.User1.name;

string y will then become "Bob", from the object I made in the otherScript. But I want to be able to change which "object I reference. For Example:

string x; // This string will change, could be User1, User2 etc...

otherScript.x.name;

The above line, will return an error. It will look instead for a name variable attached to the string I made in THIS script. Is there a way to use a string variable where the x is, that will just change the object I'm referencing?

What I have tried:

I've tried passing the string through a function written in the script containing the object, and even then it won't work. Or putting the string in brackets.

otherScript.(x).name;
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900