- Home /
initialize with object name
Hi, I have a field in a class(named ngut) like these:
public string gsm;
Now the value of this field must be initialized when creating an instance of the respecting class. I want to initialize gsm field with object's name automatically. Thats it, after creating an instance, gsm should contain "X"(where X is the name of instanced object) string value. I set up constructor for this. But,
I do not want this,
ngut sample=new sample("sample");
I want to simply write,
ngut sample=new sample();
And gsm field automatically populate with "sample" value. Can this be done anyhow?
In what way does this object you are creating have a name? Do you mean the name of the type? Do you mean that sample is the name of a class derived from ngut?
@whydoidoit, Suppose the class is this:
public class ngut {
private $$anonymous$$eyCode kb;
private $$anonymous$$eyCode joy;
private string gTag;
private string gLayer;
private string $$anonymous$$name;
public ngut()
{
//initialize the field
}
public ngut($$anonymous$$eyCode kbArg,$$anonymous$$eyCode joyArg)
{
//another overloaded constructor.
}
public void a$$anonymous$$ethod(){}
public bool another$$anonymous$$ethod(){return true;}
.........................................
.........................................
.........................................
}
Now I want to create an instance of this class, and name the object to "sample". So I do like,
ngut sample = new ngut();
You see there is a field called "$$anonymous$$name". I want to do something on a constructor which will make the value of "$$anonymous$$name" equal to "sample".
Lets say I created another instance of this class. The name of object is "sampleTwo". So I will write by,
ngut sampleTwo = new ngut();
You see I did not initialize $$anonymous$$name field. But I want to write something on constructor code block( yes within "{}" block, Not the "()" block as argument), something sothat I do not have to initialize within "()". Normally every time I create an object I have to write like,
ngut sample = new ngut(string $$anonymous$$nameArg);
ngut sampleTwo = new ngut(string $$anonymous$$nameArg);
This way user have to pass a string value on constructor, every time he/she has to initialize $$anonymous$$name. And Also he/she has to be careful sothat string value matched the object's name he/she created. This regulation is required for artist's sake. As we know for sure that object's name=$$anonymous$$name field, I wanted to automate this. I tried something like this:
public class ngut {
private $$anonymous$$eyCode kb;
private $$anonymous$$eyCode joy;
private string gTag;
private string gLayer;
private string $$anonymous$$name;
public ngut()
{
//initialize the field
$$anonymous$$name=this.name;
}
public ngut($$anonymous$$eyCode kbArg,$$anonymous$$eyCode joyArg)
{
//another overloaded constructor.
}
public void a$$anonymous$$ethod(){}
public bool another$$anonymous$$ethod(){return true;}
.........................................
.........................................
.........................................
}
But it didn't work. Basically when user will use ngut class, I want them to create instances of ngut class. The $$anonymous$$name field will be automatically initialized. Is this even possible?
I wanted this sothat users do not have to write code for initializing $$anonymous$$name field.
@kaiyum: The problem with this is best illustrating by the fact that you could easily do new ngut();
without actually declaring a variable. The instance of the class is created before its reference is set to the variable. The class constructor has no way of knowing what you are going to do with the instance. Besides, what's stopping anyone from reassigning it to different variable, making multiple references, using an array element, a derived class, etc.
There is may be a way to reliably use reflection to find out what variables in the assembly are declared with your class type and find your class's reference. But it still won't work in constructor, because the reference is not yet set to that variable there.
hmm, so users of class should be more careful while creating objects then.
Answer by Deon-Cadme · Feb 16, 2014 at 09:19 AM
Hi,
I would recommend that you read up on static class members. They can easily deal with your problem as they are variables and arrays etc that exist at the same time in all objects that contain the class :)
I would personally just create a "static int count" variable. Increase count with one every time an object is created with the class and add it to the name like: "sample0", "sample1" and "sample2" etc.
Just note that you cannot decrease count if you randomly destroy the scene objects because that will cause duplicate names to appear. Just increase and add... ints are pretty big so I doubt that you will reach the limit ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613133658im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to initialize lists with set number of null elements? 1 Answer
Formatting Data Classes 3 Answers