- Home /
All Sprites Instantiate In top right of screen for some reason.
So I'm working on a simple "chain reaction" game (you click a single dot per game, it explodes detonating nearby dots and you try to get this biggest chain possible) I got my points to move around correctly, and made a small debug menu to change startup parameters. I spawn objects at 2 points in the code.
1
void checkBounds()
{
if(spr.isVisible==false)
{
//spawn a new object
Vector3 newPos=Camera.main.ScreenToWorldPoint(new Vector3(Screen.width,Screen.height,0));
Instantiate(Point,new Vector3(Random.Range(0,newPos.x),Random.Range(0,newPos.y),0),this.transform.rotation);
Destroy(this.gameObject);
}
}
This basically checks if the sprite is off screen, if it is then it destroys it and spawns a new one within the bounds.
and 2
public class spawner : MonoBehaviour {
public float count;
public GameObject Point;
// Use this for initialization
void Start ()
{
levelData data=GameObject.Find("mainObj").GetComponent<levelData>() as levelData;
count=data.count;
}
// Update is called once per frame
void Update ()
{
Vector3 newPos=Camera.main.ScreenToWorldPoint(new Vector3(Screen.width,Screen.height,0));
float counter=count;
while(counter!=0)
{
Instantiate(Point,new Vector3(Random.Range(0,newPos.x),Random.Range(0,newPos.y),0),this.transform.rotation);
counter--;
}
Destroy (this.gameObject);
}
}
For some reason i cant figure out this code spawns all the points in the top right of the screen clustered together.
I'll throw in a web player as well in case you want to see it in action (although its nothing too exciting) however you Must change the count, It says 1 from the start but it must be changed for the value to stick.