- Home /
Can a c# script in unity be coded to alter itself?
I'm using it for something very simple, but hypothetically this concept could be applied to very complex scripts that basically "learn" from the player. Can I incorporate variable strings in a c# code so that the strings can then refer to or be stored as variable names for later use? Here's my current script:
public void SendBird (GameObject bird)
{
PigeonController pigeonController = bird.GetComponent<PigeonController>();
string recipient = pigeonController.recipient;
string obtain1 = (recipient + "Imp");
string obtain2 = (recipient + "Act");
string obtain3 = (recipient + "Tar");
obtain1 = assignWords.imperative;
obtain2 = assignWords.action;
obtain3 = assignWords.target;
}
in this case, I have six possible recipients: soldier, medic, sniper, gunner, tank, and air, who all can receive an imperative, an action, and a target. The reason pigeons/birds come into play is that messages with the three word types are sent to those soldiers on the battlefield by the player. Each of the six birds has a string variable called "recipient" in its PigeonController script; this string is like a tag (the reason it is not a tag is because the tag for each is "Pigeon"). "assignWords" refers to the script of the letter being handed to the bird which contains the associated words.
Could I set up something like the above to take the recipient string, add the word type to it, and then use that new string to access string variables like "airImp" or "tankAct" or "soldierTar"? It would be much less data than having to have six if statements, each with several lines outlining which string transfers where.
You CAN create your own state-machine: like a virtual computer. Such a state machine would perform various operations based upon, and using, the data: but you would need to define this data->operation relationship exactly (effectively an op-code set), which can be quite a large task.
Answer by Bunny83 · Dec 01, 2016 at 07:56 PM
No, scripts are compiled to IL code so the final version of your game doesn't even contain any source code. The question is why you actually have 6 different classes or variables for the same kind of information? You may want to read some material about inheritance and polymorphism.
We don't know how your PigeonController class actually looks like and where / in which was you need that information. If you want a more specific answer you should edit your question and add more details.
PigeonController.recipient is actually just a public string that is different for each of the six pigeons. Each pigeon sends a different message to a different part of the GameController (not shown) which sets a different set of reactions in motion which then returns six distinct messages to the player for each of the six pigeon recipients.