- Home /
AddComponent() is adding 64 instances of my script to my GO
I am using this code to instantiate one gameobject with a script Ground attached to it:
public void SpawnInitial()
{
int loadDist = GetComponent<GlobalWorldValues>().loadDist;
GameObject[] originals = Resources.LoadAll<GameObject>(midPath);
GameObject original = originals[UnityEngine.Random.Range(0,originals.Length)];
Vector3 pos = new Vector3(transform.parent.transform.position.x,-original.GetComponent<SpriteRenderer>().bounds.extents.y,0);
Ground g = original.AddComponent<Ground>();
print("added g to original");
g.isReversed = false;
g.isSloped = false;
g.path = midPath + "/" + original.transform.name.Replace("(Clone)","");
Instantiate(original,pos,Quaternion.identity);
}
It's compiling just fine no errors. But when I hit play I'm met with this in the hierarchy:
It's adding 64 instances of this script to my object. I've counted and there are only 4 instances of AddComponent() to ANY object in every script that even has the ability to run this method. And none of them are in any kind of loop. Needless to say, I'm so confused, any insight would be appreciated. If you need more info just ask I'll be glad to post more. Thanks.
Answer by HarshadK · Apr 30, 2015 at 05:38 AM
It might be happening because you are actually adding the component to the prefab itself which is loaded from Resources. I would say first Instantiate the gameobject and then add the component to it as below:
public void SpawnInitial()
{
int loadDist = GetComponent<GlobalWorldValues>().loadDist;
GameObject[] originals = Resources.LoadAll<GameObject>(midPath);
GameObject original = originals[UnityEngine.Random.Range(0,originals.Length)];
Vector3 pos = new Vector3(transform.parent.transform.position.x,-original.GetComponent<SpriteRenderer>().bounds.extents.y,0);
GameObject clone = (GameObject) Instantiate(original,pos,Quaternion.identity);
Ground g = clone.AddComponent<Ground>();
print("added g to original");
g.isReversed = false;
g.isSloped = false;
g.path = midPath + "/" + original.transform.name.Replace("(Clone)","");
}
Unfortunately that doesn't work either. But it leads me to believe the error is elsewhere.
Can you confirm that the prefab has no Ground components to start with? If it's ended up with 63 then no code changes are going to work till you fix the prefab. To be sure I would protect the original.AddComponent...
with a check like if (original.GetComponent... == null)
(and log an error if the check fails).
Oh man, your comment led me to discover that I'm not doing what I wanted to do. There were a bunch of Ground scripts attached to the prefabs, but I don't want them to be attached to the prefab. I just want them to be attached to the instance when I spawn it, then destroy it when I'm done with it, recovering the Ground script data.
And I believe those 63 scripts were added when you ran your previous script (the one in the question) that was adding component directly to the prefab.
Yea I just gave up trying to add an instance of a $$anonymous$$onobehaviour to a prefab and just had them have it as a component by default