Concatenate Class Instance Reference Label

How might I go about renaming “t” to something that say increments for every new Transceiver that is created. So like, the first instance reference is labeled “t1” and the next is “t2” without hard coding it I mean. Can I concatenate the reference name upon creation?

In Code:

        static int transNum = 1;
        private void CreateTransmitters(int numTransmitters, string classifier)
        {
            string label = "Transmitter" + transNum.ToString();
            Transceiver label = gameObject.AddComponent<Transceiver>();
            transmitters.Add(Transmitter + transNum.ToString());
            transNum++;
        }

How would I do something like this… Normally people know how many instances they’ll need and just label them manually… I’ve not yet seen anyone label references dynamically.

You can just have a static integer value that your constructor calls. It’s a fairly common pattern.

static int transNum;

Then in your ctor don’t accept transNum but instead just increment the static member before (or after) you use it for the label.

If you need to reset it periodically (like at a new scene), make a method to reset it and then you can keep the implementation details contained to the class.

My example code had a couple tweaks needed, so I just updated that.

Yes, I see you changed it. Just make transNum static and increment it and you’re done.

I’m having trouble because when I am Declaring the reference it doesn’t appear to be accepting “label” as a valid name for the reference. I get error: “CS0128 A local variable or function named ‘label’ is already defined in this scope.”
And when I get down to “transmitters.Add(“Transmitter” + transNum.ToString());” is give me error: CS1503 Argument 1: cannot convert from ‘string’ to ‘Transceiver’

And this is after making “transNum” a static variable.

first of all on lines 5 and 6 I’m declaring and assigning a string, then trying to initialize a reference by that string… but I’m not sure 100% on this, but I’m pretty sure a reference is not of type “String” it is something else, but I can’t figure it, but if I can, maybe casting it to that proper type might work? then I can reference “label” directly when I’m adding it to the List… I’m not sure… it’s something syntactical though… I know that.

I don’t want to make the wrong guess as to what you’re trying to do. Could you try to reword it a bit more plainly so I’m sure?
I can see why you have issues, but I’m not sure about your end goal.

I will be instantiating multiple instances of said “Transceiver” Class. basically as each “Transceiver” Class is Instantiated, I need every new reference to be dynamically labeled by a string so say I make 3 instances of Transceivers, I need to be able to reference them as:
“Transmitter1”
“Transmitter2”
“Transmitter3”
The tricky part is that the number of these Transceivers will be dynamically added to, and removed from during Runtime, (thus why they are stored in a list) so I can’t simply hard code a number of instances with per-defined reference names.

Ultimately I would like to be able to reference them by more than just arbitrary single digit numbers, but the single digits are just for testing purposes.

Right, well when you say label… do you mean the game object name or some property/field on the class, or what? :slight_smile: That’s maybe part of what I’m a little fuzzy on.

I got you, so what label is supposed to be, is the dynamic name which is assigned to the newly created instance of “Transceiver” every time the “CreateTransmitter” method is called. obviously my syntax is off here, because it doesn’t work, but ultimately, from my research, I believe the equivalent outcome I’m looking for is that of MS’s Activator.CreateInstance() method where (based on which overload you use of course) you pass the namespace directive, then the instance name as a string, in this format I would be able to instantiate an instance using a string for the reference name… label is just meant as that “string for the reference name”… I’m not sure the proper syntax in Unity to implement the same dynamic type of functionality.

Ya… My knowledge in that area is not great, but I think it’s safe to say that if they are instances of transceiver, create instance would be making tranceivers…

Let me ask a different question. In what sense do you need a unique name and for what purpose?
Maybe that will help me (or someone else) answer and/or explain something better.

Another reference article I’ve been attempting is here.

in usage, I eventually will need the label to be an outcome which represents a 3 dimensional location i.e. “Transmitter[1.34, 53.2, 32.1]” or you know… something that uniquely identifies it in a way that I can later parse the reference name into sections, and translate the data as needed to determine what should happen to the instance based on other conditions… if that made sense… I’ve reread it to try to explain my best… but truly Transmitters and Receivers are only two examples of the project, this is actually a functionality which will be scaled throughout my whole program. I don’t use Unity’s transform for many reasons, but namely because I 1.) need it to be able to run without graphics after I’ve tested functionality, and 2.) re-write it in a more “barebones” language later, thus it cannot rely upon Unity specific methods and such.

For example of what I’m doing full-scale without immense detail, I made my main class, which then instantiates multiple instances of child class, with identifiers, and storing them in a list, then each child class also instantiates multiple instances of “Transceiver”, uniquely identifying each of those, and also storing those in a private list. And each Transmitter/Receiver… yeah… they transmit data and receive data accordingly, and pass it around based on conditions.
It’s a code wise, pretty straightforward project, but the structure is meant to be entirely environment specific, and able to dynamically alter values and whatnot, which in turn affects the over all number of instances of sub-classes… deep breathe

yeah… that.

Still totally lost on your terminology of a label. But you want a Vector3, maybe I’m just confused on why you don’t just store that in the class?

Nothing you’ve explained to me has me thinking you need more than that, but I’m not completely confident I understand you, so there is a margin for error :slight_smile: :slight_smile:

label is just a string which will define the name of the reference. every time I call the Method “Create” it ups the int and adds that int (as a string) to the end of the reference name. then it is supposed to create a new reference to that instance, and the reference should be named as what ever “label” is equal to. I.E. "Transmitter " + intVal.ToString()
say intVal = 3;
label would then be redefined as the string type “Transmitter 3”
so when I make a reference and instantiate the new instance, it is suppose to make a new instance who’s nameis “Transmitter 3”

thus, “Class label = gameObject.AddComponent()”
-----------^---------^------------------------------------------------^
Script Ref----Ref Name---------------------------Instantiate the Script

same as manually typing "Transceiver Transmitter3 = gameObject.AddComponent();

but using label is supposed to allow me to change that number each time I call the Creat method. Or perhaps pass the unique identifier as a parameter.

then I can later say something like Transmitter3.VartoChange = blah; or kill it entirely.

well, like where would you store these? these label names…

I’m just still confused, sorry.

In a list/array, you could just as well do : transmitter[3].varToChange = blah;

Now I’m sure I know what you’re talking about, just not sure why you’d need that. anyways, it’s not for me to put ya down, if there’s a way to do it… it’s your choice :slight_smile:

hmmm… so after thinking about it around again, I realized I can use a dictionary… for now… idk what equivalents there are in say, python, but I’m hoping there’s something. Anyhow, here’s what I came up with, with one minor issue.

        public Dictionary<Vector3, Transceiver> transmitters = new Dictionary<Vector3, Transceiver>();
        private void CreateTransmitters(int numTransmitters, string classifier, Vector3 location)
        {

            Transceiver transmitter = gameObject.AddComponent<Transceiver>().Init(classifier, Transceiver.Type.Trans);
            transmitters.Add(location, transmitter);
        }

Without getting into customEditor stuff, is there a simply way to visualize a Generic like Dictionary that you’ve found?

As for the part

As for this, the whole point what that I didn’t want to store the identifier separate from “Transmitter” the whole reference name was supposed to become one (thus, “Concatenate” in the thread name). but the number updates, however, considering your point on identifying them by a separate value entirely, rather than it being in the name, I believe I get what you were going for with using Vector3’s and referencing it by the Vector3 value… (again, not what I was aiming, but hey, looking at it, as long as that value is coupled to a particular instance I suppose that’d work fine)