- Home /
Question by
mipoxaandrew2014 · Jul 17, 2019 at 07:46 PM ·
instantiatebug
Instantiate spawns the object incorrectly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGeneration : MonoBehaviour
{
public GameObject Player;
public GameObject Snow;
public GameObject Camera;
public float Xcor;
public float Ycor;
// Start is called before the first frame update
void Start()
{
Xcor = transform.position.x;
Ycor = transform.position.y;
}
// Update is called once per frame
void Update()
{
if (Camera.GetComponent<CameraScript>().Tiles.ContainsKey((Xcor + -1.83f) * 10000f + Ycor + 1.05f) != true && -20 < Xcor && Xcor < 20 && -20 < Ycor && Ycor < 20)
{
Instantiate (Snow, new Vector3(Xcor + -1.83f , Ycor + 1.05f, 0f), Quaternion.identity);
Camera.GetComponent<CameraScript>().Tiles.Add((Xcor + -1.83f) * 10000f + Ycor + 1.05f, "Snow");
}
if (Camera.GetComponent<CameraScript>().Tiles.ContainsKey(Xcor * 10000f + Ycor + 2.1f) != true && -20 < Xcor && Xcor < 20 && -20 < Ycor && Ycor < 20)
{
Instantiate (Snow, new Vector3(Xcor, Ycor + 2.1f, 0f), Quaternion.identity);
Camera.GetComponent<CameraScript>().Tiles.Add(Xcor * 10000f + Ycor + 2.1f, "Snow");
}
}
}
This code should spread the snow, it does it correctly a few times and then starts spawning new snow at +0.000001X which completely messes up the spawning algorithm, doubling the amount of snow each time it does so. The snow is static so the issue seems to be Instantiate working wrong.
Comment