Access a script with file path?
I have an Account system that creates a c# script in a database for every account. Now I'm trying to make a login system that can access the variables of an account script.
What I want to do is either reference the script at the target path
So that i can access the variables of the account script like: "ExampleScript.accountBalance"
Or if there is another way, like adding the target script as a component on the "player" game object.
I've tried googling and searching here but I can't find a way to load scripts from a path. Does anyone have any tips?
When i enter a username my script creates the following path
string path1 = @"Assets/Database" + "/" + accountNameString +".cs";
Sorry for this unhelpful comment, but I think you've got a serious design flaw there. If you only need to store some variables for each player, why don't you just serialize and save that data ? You could use PlayerPrefs for easily storing and retrieving player data.
Ex : you store data with PlayerPrefs.SetFloat(playerName + "balance", 17.2f);
and retrieve it later this way :
float GetAccountBalance(string playerName)
{
if (PlayerPrefs.Has$$anonymous$$ey(playerName + "balance"))
{
return PlayerPrefs.GetFloat(playerName + "balance");
}
return 0;
}
Unless I'm completely missing the point, your idea is an overkill (and unfeasible ?) solution to a quite simple problem : storing data.
Hope that helps.
Thanks for the reply.. I'm very new to C# and program$$anonymous$$g in general so whatever I've done it's probably not very good... :D
But I just can't see how your solution saves data permanently, If i store data like that and then turn the game off, i will have lost my progress, right?
$$anonymous$$y intention was to save a few variables in an external database, such as: First Time Logging in, (To enable tutorial or tips) Email, Username, Password, In game currencies, items etc.
Within my "account scripts" there is only a variable declaration.
The way i save data right now, is by writing and rewriting lines in the variable declaration, so they are always saved permanently.
And to the original question: wanted to know how to load a script from a file path.
Like Resources.Load, but ins$$anonymous$$d of loading from resources, i want to load from any file path. I know I was a bit unclear the first post, but I hope this makes more sense...
Your answer
![](https://koobas.hobune.stream/wayback/20220612063239im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My GameObject disappears when running this script. 0 Answers
The laser problems 0 Answers
Add 2D Skeleton to the Scene if Button was pressed 0 Answers
Integer arrays not comparing properly. 3 Answers
how do i take whats on one UI canvas and make it apear on another in real time(for a card game) 0 Answers