- Home /
instanstiate Spawns more then one objec
This is part of my code
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class PrefabSpwaning : MonoBehaviour {
public GameObject Ground;
float playerr;
// Use this for initialization
void Start () {
if (PlayerPrefs.GetInt("Player") == 0)
{
player = GameObject.Find("Male(Clone)");
}
else if (PlayerPrefs.GetInt("Player") == 1)
{
player = GameObject.Find("Female(Clone)");
}
}
// Update is called once per frame
void Update () {
if (GameObject.Find("Female(Clone)").GetComponent<Control>().ground == true)
{
playerr = player.transform.position.x;
Instantiate(Ground, new Vector3(playerr+150, 0, 0), Quaternion.identity);
GameObject.Find("Female(Clone)").GetComponent<Control>().ground = false;
}
else
{
GameObject.Find("Female(Clone)").GetComponent<Control>().ground = false;
}
}
} public class Control : MonoBehaviour { } void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Ground") { ground = true; } } }
So on the player there's a OnTriggerEnter. If the player collides with the object it will set the the ground to true. Now in the Update of another script, I check if ground is true on the control script. If it is true it should spawn a ground. This works, however it will spawn one ground first then spawns two second time and then three. I only want it to spawn one ground constantly. This is a endless runner game. So everytime the player enter the collide a ground should spawn in front of it but instead multiple ground is spawning which is then killing the performance of the game. The maximum amount of ground is spawning is 4. On the computer it plays alright but on a mobile it lags badly if more then one ground is spawned. I want to know why it is spawning multiple grounds.
Your answer
Follow this Question
Related Questions
How to Spawn after checking if the clones are destroyed. 1 Answer
Prevent object from spawning at a spawn point twice in a row? 3 Answers
how to destroy a object only in game and not the prefab 2 Answers
Have a spawning issue on iOS 0 Answers
UnityException: Transform child out of bounds error when using 2 prefabs in tile manager script. 0 Answers