Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by CatStrategist · Nov 21, 2015 at 12:23 AM · unity 5instantiatespawning

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges