bearing in mind, I’m super tired right now, I may be off here, I just find it quite suitable to be able to pass in conditions at the beginning of a script starting up. It’s not so much that I’d want to manually call a starting method, so much as I’d like to be able to you know… .more or less tell the script “how to start” based on the parameters I throw in with the “startup” command. The simple get around I’ve found is renaming all my constructors to “Init” then after adding them as components, calling the “init” method on it. But it just seems a little… idk. tedious… here, I’ll show you a snippet of my code so you see my usage.
Transceiver
public class Transceiver : MonoBehaviour
{
public Dictionary<Classifier, float> storage = new Dictionary<Classifier, float>();
public enum Type { Recvr, Trans }
public enum Classifier { one, two, three, four, five }
//Rename to Constructor for C# Only runtime.
public void Init(string classifier, Type transceiverType)
{
//IF CLASSIFIED AS "ONE" STRENGTH IS 1
//IF CLASSIFIED AS "---" STRENGTH IS -
switch (classifier)
{
case "one":
//storage.Add(Classifier.one, 1);
print("Added 1");
break;
case "two":
//storage.Add(Classifier.two, 2);
print("Added 2");
break;
case "three":
//storage.Add(Classifier.three, 3);
print("Added 3");
break;
case "four":
//storage.Add(Classifier.four, 4);
print("Added 4");
break;
case "five":
//storage.Add(Classifier.five, 5);
print("Added 5");
break;
default:
throw new System.Exception("Sorry, must use default options...\n");
}
}
}
^ That is the script I’m calling. ^
Input
public class Input : MonoBehaviour
{
public List<Transceiver> receivers = new List<Transceiver>();
//Rename to constructor for C# Only Runtime.
public void Init()
{
//I'm manually calling the create method because I'm only at the beginning phases and doing testing.
CreateReceivers(2, "three");
CreateReceivers(1, "five");
CreateReceivers(4, "two");
}
private void CreateReceivers(int numReceivers, string classifier)
{
receivers.Add(gameObject.AddComponent<Transceiver>());
this.gameObject.GetComponent<Transceiver>().Init(classifier, Transceiver.Type.Recvr);
}
}
^ That is the Script which calls the first one ^
Now the brief over view of this is that my Input creates a Transceiver and in doing so, it defines “Classifier” as well as whether the Transceiver will function as a Transmitter or a Receiver. In regular C# I can simply do
new Transceiver(classifier, Transceiver.Type.Recvr
but for MonoBehaviour I’m stuck renaming my constructor, then throwing in 2 extra lines of code… as I said, it’s just tedious to modify it, especially since I’m only using Unity to be able to visualize the activity, and the final product will not actually really even have graphics. I’ll be going backwards to re-model everything later, after I know my over all project model functions correctly.