Gameobject duplicate in simple loop


I’m trying to create a gameobject in one script, and loop that process in another. But when i’m doing this i’m getting duplicate gameobjects ‘New Game Object’ in the hierarchy.

Not too sure what’s causing this really. Is it something to do with that i’m returning a gameobject in a loop? Is feels like it’s instantiating it twice inside my helper (script 1)

The actual function works, and I get my buttons in my canvas. But there’s the objects without parents still being a nuisance

Would really appreciate some help,

//Script 1
public GameObject DrawButton(string text, Rect rect, Vector2 pivot, float alpha)

        GameObject img = GameObject.Instantiate(Resources.Load("button")) as GameObject;
        img.transform.SetParent(canvas.transform); = "btn";

return img;


And I’m calling that from another script:(please forgive the awful naming conventions so far)
foreach (Choice c in choices.choices)

            diagChoices.Add(DrawButton(c.text, new Rect(Screen.width / 2, (Screen.height / 4) + y, Screen.width / 1.5f, 30), new Vector2(0.5f, 0.5f), 1));
            y -= 40;

So far your code shouldn’t do duplicate Game Objects. Even if it does, you should see a duplicate “btn”, not “New Game Object”, it has to be something else