- Home /
Having problem with Instatiate GameObject at Canvas
hello I'm making a some 2D button games.
But I have problem with Instatiate Gameobject at Canvas
here's my simple code
public class SpawnPosition : MonoBehaviour { public GameObject BlueRectPrefab; public GameObject BlueRectSP; // Use this for initialization void Start () { Instantiate(BlueRectPrefab, BlueRectSP.transform.position, BlueRectSP.transform.rotation);
}
}
I just want my prefab to instantiate at canvas
but when i press play button
nth spawn at my play screen
I checked settings but still got what is problem.
https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html
I read this manual
this manual says setting transform.setparent to false can instantiate the prefab at canvas
but i have still no idea how to do it.
How can i solve this problem???
Thank you for reply and sorry for my unkind code. Well.. I'm not familiar to unity community writing system.
To go on.
$$anonymous$$y code is
GameObject BRect;
BRect = Instantiate(BlueRectPrefab, BlueRectSP.transform.position, BlueRectSP.transform.rotation) as GameObject;
BRect.transform.SetParent(BlueRectPrefab.transform, false);
I put this code at void start
and the object should be in canvas
like
main camera
canvas
object what i want to spawn(inside canvas)
but it works like this
main camera
object(clone)(not in canvas)
canvas
how can i solve this problem?
oh I solved this problem
setting code
like this:
BRect.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform.GetChild(0), false)
thanks for reply :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612093751im_/https://answers.unity.com/themes/thub/images/avi.jpg)