Problem instantiating objects one after another with mouse click
I am building tower defense game and I want to spawn a lot of objects with continous mouse clicks until I cancel building mode with right click. I have a button which calls function instantiating object. Created object follows mouse cursor until left mouse button is pressed, then it stays in place and another one is instantiated who should as well follow cursor until left key is pressed. At least this is what I want to achieve, but I don't know why, and how when I press left button two objects are instantiated. Below is a code which contains method InstantiateObject called by button with object to spawn as argument.
using UnityEngine;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
public enum GameState { normal, build, destroy };
public static GameState gameState = GameState.normal;
void Start()
{
}
public void ChangeGameState(GameState state)
{
gameState = state;
}
public void InstantiateObject(GameObject objectToInstantiate)
{
if (gameState == GameState.normal)
{
ChangeGameState(GameState.build);
Vector3 v3 = Input.mousePosition;
v3 = Camera.main.ScreenToWorldPoint(v3);
v3 = new Vector3(Mathf.Floor(v3.x), Mathf.Ceil(v3.y), 0);
Instantiate(objectToInstantiate, v3, Quaternion.identity);
}
else if (gameState == GameState.build)
{
ChangeGameState(GameState.normal);
}
}
}
And here is script of instantiaded object.
using UnityEngine;
using System.Collections;
public class BuildingBehavior : MonoBehaviour {
GameObject gameManager;
GameManager gmScript;
public GameObject objectToSpawn;
// Use this for initialization
IEnumerator Start() {
GameObject gameManager = GameObject.Find("_GameManager");
GameManager gmScript = gameManager.GetComponent<GameManager>();
yield return StartCoroutine(spawnLoc());
}
// Update is called once per frame
void Update () {
}
IEnumerator spawnLoc()
{
while(GameManager.gameState == GameManager.GameState.build)
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(objectToSpawn, new Vector3(0, 0, 0), Quaternion.identity);
yield break;
}
else if (Input.GetMouseButton(1))
{
GameManager.gameState = GameManager.GameState.normal;
GameObject.Destroy(this.gameObject);
}
Vector3 v3 = Input.mousePosition;
v3 = Camera.main.ScreenToWorldPoint(v3);
v3 = new Vector3(Mathf.Floor(v3.x), Mathf.Ceil(v3.y), 0);
transform.position = v3;
yield return null;
}
}
}
In place of objectToInstantiate in second script I tried reaching method in GameManager and pass object, I tried making list of objects and refering to a prefab which one to spawn and nothing worked it always spawns two objects on top of each other with every mouse click. Any ideas how to fix this?
Answer by CatStrategist · Nov 21, 2015 at 04:14 PM
I fixed it myself, for anyone who will have the same problem: before instantiating object on mouse0 down you have to wait for a frame to end after condition is checked (yield return new WaitForEndOfFrame()) because the checked condition is passed to a next object and will also execute another Instantiate.
Your answer
Follow this Question
Related Questions
NetworkServer.Spawn() only on Server (with registered prefabs) [Unity 5.2.3f1] 0 Answers
Instantiate not working correctly (Please help!) 1 Answer
Instantiate is not working in unity 5.6.4f1 0 Answers
Spawn Object under Moving Player 0 Answers
How do i Instantiate sub-Points with in Multiple Points?? 0 Answers