- Home /
game object instantiate questions
I'm having trouble with some of my scripts. My project is 2D. GameObjects instantiated don't appear at the specified coordinates, unless the transform position is specified. GameObjects instantiated and then made children of other GameObjects, with a position of (0, 0) don't appear relative to their parent GameObjects. Also, the camera in my scene is not pointing at the GameObjects that my scripts are creating, at least the game view doesn't show any of the game objects created. I also can't seem to figure out how to rotate the view gizmo when in 3D mode. Any help?
for (int indexY = 0; indexY < y; ++indexY)
{
for (int indexX = 0; indexX < x; ++indexX)
{
int posX = (indexX - (maxX / 2));
int posY = (indexY - (maxY / 2));
var back = (GameObject)Instantiate(background);//, new Vector2(x, y), Quaternion.identity);
back.GetComponent<SpriteRenderer>().enabled = true;
back.transform.position = new Vector2(posX, posY);
char elem = map[indexX, indexY];
if ((elem == '|') || (elem == '-'))
{
var wll = (GameObject)Instantiate(wall);//, new Vector2(0, 0), Quaternion.identity);
wll.GetComponent<SpriteRenderer>().enabled = true;
//wll.transform.parent = back.transform;
wll.transform.position = new Vector2(posX, posY);
}
else if (elem == '.')
{
var flr = (GameObject)Instantiate(floor);//, new Vector2(0, 0), Quaternion.identity);
flr.GetComponent<SpriteRenderer>().enabled = true;
//flr.transform.parent = back.transform;
flr.transform.position = new Vector2(posX, posY);
}
else if (elem == '@')
{
var plyr = (GameObject)Instantiate(player, new Vector2(0, 0), Quaternion.identity);
plyr.GetComponent<SpriteRenderer>().enabled = true;
//plyr.transform.parent = back.transform;
plyr.transform.position = new Vector2(posX, posY);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613145810im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Make camera follow an instantiated object 1 Answer
Spawning problems with cameras 1 Answer
Gun is shooting based on movement not camera rotation? 1 Answer
GameObject Instantiated Off Camera 0 Answers