Instantiating wierd problem

i m working on a game where i need to instantiate three random platforms in three columns, but the problem is that sometime the code works properly but sometimes 1 to 2 of my platforms spawning in center of screen … below is my code

	public GameObject[] platforms;

	public GameObject playerObject;

	public uint blockCount;

	private Vector3 spawnPos;

	private GameObject[] lastObjects;

	private string goName;
	private uint nameInt;
	private uint j;

	private Vector3 lastVec;
	private Vector3 worldPosVec;

	void Start(){
		blockCount = 0;
		lastObjects = new GameObject[]{platforms[0], platforms[1], platforms[2]};
		spawnPos = new Vector3 (0f,-7f,0f);
		lastVec = new Vector3 (0f,0f,0f);
		worldPosVec = new Vector3((10f / 100f) * Screen.width,(90f / 100f) * Screen.height, Camera.main.farClipPlane);
		lastVec = Camera.main.ScreenToWorldPoint (new Vector3(0f, -600f, Camera.main.farClipPlane));
		goName = "init";
		nameInt = 1;
		j = 1;
		spawnPos = Camera.main.ScreenToWorldPoint (worldPosVec);
		spawnLeftLine ();
		Instantiate (playerObject, Camera.main.ScreenToWorldPoint(new Vector3((10f / 100f) * Screen.width, (20f / 100f) * Screen.height, Camera.main.farClipPlane)), Quaternion.identity);
	}

	void spawnLeftLine(){
		if (blockCount == 0) {
			for(uint i = 0; i < 3; i++){
				j =(uint) Random.Range(0f,3f);
				switch(i){
				case 0:
					lastObjects *= (GameObject)Instantiate(platforms[j], spawnPos, Quaternion.identity) as GameObject;*
  •  			goName = "blockLeft";*
    
  •  			nameInt = 1;*
    

_ lastObjects*.gameObject.name = goName.ToString() + nameInt.ToString();_
_
break;_
_
case 1:_
_ worldPosVec.x = (50f / 100f) * Screen.width;
worldPosVec.y = (10f / 100f) * Screen.height;_

_
spawnPos = Camera.main.ScreenToWorldPoint(worldPosVec);_
_ lastObjects = (GameObject)Instantiate(platforms[j], spawnPos, Quaternion.identity) as GameObject;
goName = “blockMid”;
nameInt = 1;
lastObjects.gameObject.name = goName.ToString() + nameInt.ToString();
break;
case 2:
worldPosVec.x = (90f / 100f) * Screen.width;
worldPosVec.y = (90f / 100f) * Screen.height;
spawnPos = Camera.main.ScreenToWorldPoint(worldPosVec);
lastObjects = (GameObject)Instantiate(platforms[j], spawnPos, Quaternion.identity) as GameObject;
goName = “blockRight”;
nameInt = 1;
lastObjects.gameObject.name = goName.ToString() + nameInt.ToString();
break;
}
}
blockCount = 3;
}
}*

the platforms are setup in such a manner that it has a parent Gameobject with spriteRender component on it and nothing else, a child gameObject with a collider on it and nothing else and another child gameObject with Trigger Collider on it and nothing else_

You set the worldPosVec in case 1 and case 2, you don’t set the worldPosVec in case 0. So it will use the value as defined on line 23, which is exactly the same as on line 54 and 55 of case 2.

You need to take more care when writing your code. Following the program flow and checking the state of values is an important aspect of programming. Use compiler debugging and Debug.Log() to help in your efforts.

Hi aditya

I’ve gone over your code and can not instantly see any problems with it, assuming the code you’ve shown is the only code that spawns platforms. I’m guessing that you are expecting spawnLeftLine to only ever do something once, as it only spawns platforms if blockCount is 0, and you set it to 3 as soon as you’ve spawned some. As the guys in the previous answer suggest, you approach isn’t the tidiest, but doesn’t appear to be broken, and if you’ve verified the spawn positions are correct with debug prints then we have to assume its something else.

Given there’s nothing obviously wrong with the code (though I could certainly offer some constructive criticism as to how to write some of it a little better - let me know if you’d like that), my guess is that it could be something to do with your platform prefabs. Could I suggest trying restricting it to only ever spawn platform 0, then try restricting it to only ever spawn platform 1 etc. Perhaps one of those platforms is causing issues, and this will help identify which one.

FYI, I’m currently going on the assumption that you have no other code that might set the position of platforms or spawn platforms. If that’s incorrect, we’d need to verify your other code to identify if the problem is elsewhere.

-Chris

lastObjects = (GameObject)Instantiate(platforms[j], spawnPos, Quaternion.identity) as GameObject;
lastObjects*.transform.position = //etc*
As a last resort/backup simply reposition the objects after Instantiation.

thanks @centaurianmudpig @Bonfireboy @wibble88 and @meat5000 for your quick replies, after the answer of @centaurianmudpig i rewrite the whole program again and i don’t know how (magically) the program working all fine, but only on PC and i m facing the same problem on my android phone (ASUS zenfone 5) for which i asked this question, so now i m going with @wibble88 suggestion, as it might be the problem with platforms coz i was trying to make this game for TABs too and for that i used high resolution sprites (of about 0.7 mb for one sprite) and my phone might not getting enough time to calculate and spawn, plus the game freezes and lagging on phones … plus i m going to use the idea of @meat5000 too as i will change the positions of platforms after spawning them …