- Home /
Instantiate duplicates script in subsequent Objects
I have a simple C# function which adds a script to an Instantiated Game Object and then stores it in an array.
public void AddPort(int _group) {
//Create a new GunPort with GunPort Script attached
group[_group].Add(Instantiate(gameObject.AddComponent<GunPort>()) as GunPort);
group[_group][group[_group].Count-1].name = ("GunPort " + _group);
//Initialise the GunPort
group[_group][group[_group].Count-1].Init();
}
public void Init(Transform _owner, int _maxGroups) {
owner = _owner;
//Initialise the group array to contain the GunPort's
group = new List<GunPort>[_maxGroups];
for(int i = 0; i < _maxGroups; i++) {
group[i] = new List<GunPort>();
}
}
The problem I am having is that each subsequent script created will have n+1 GunPort scripts attached (where n is the number of GunPorts already created and stored in the array).
So, when I create 4 GunPorts ...
//..Called inside main function
AddPort(1); //1 GunPort script attached, OK
AddPort(1); //2 GunPort scripts attached, should be 1
AddPort(2); //3 GunPort scripts attached, should be 1
AddPort(2); //4 GunPort scripts attached, should be 1
What am I doing wrong?
Wow! 2018 year now. And I got this problem.
for (int i = 0; i < 3; i++)
{
/*here string path choosed according to some behavoirs*/
var newObject = Instantiate(Resources.Load<GameObject>(path));
var script = newObject.AddComponent<Hole_Color>();
newObject.transform.SetParent(transform, false);
}
Result: 1 game object - 1 script; 2 game object - 2 scripts; 3 game object - 3 scripts. Cant find explanation of this.
Answer by aldonaletto · Oct 03, 2011 at 12:07 AM
The whole thing is very confusing, but I guess that the problem is in the gameObject.AddComponent<GunPort>() instruction: it returns a GunPort object, but at the same time adds a new GunPort to gameObject!
I honestly couldn't understand what exactly you're trying to do. Are you instantiating an empty game object with the GunPort script attached? If that's the idea, I suggest the following:
public void AddPort(int _group) { // Create a new GameObject named "GunPort N", where N is number _group GameObject newGP = GameObject.GameObject("GunPort "+_group); // attach the GunPort script to it and get a reference GunPort gpScript = newGP.AddComponent<GunPort>(); // add the new GunPort object to group[_group] group[_group].Add(newGP); // Initialise the new GunPort gpScript.Init(); }
Thats exactly what I am doing, thanks for the info. Though I have used the following code to create the game object and attach the script:
GunPort gunPort = (new GameObject()).AddComponent();
Answer by Jushik · Dec 16, 2018 at 08:04 AM
Figured out!
group[_group].Add(Instantiate(gameObject.AddComponent<GunPort>()) as GunPort);
Here you added GunPort component to a gameobject which is like a prefab for Instantiate. Next time when you call AddPort
group[_group].Add(Instantiate(gameObject.AddComponent<GunPort>()) as GunPort); //gameobject already has Gunport
it is instantiates that prefab with already added GunPort component and gameObject.AddComponent()) as GunPort); adds another GunPort. I think it should be like this:
GameObject go = Instantiate(gameObject);
go.AddComponent<GunPort>()) as GunPort;
group[_group].Add(go);