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 /
avatar image
0
Question by Hobene · Apr 02, 2020 at 09:24 AM · gameobjectcoroutineprefabsarraysswipe

How to Instantiate single Prefabs from an array?

I'm trying to make a android game that is recording score with time, and the player needs to jump over obstacles as the speed get's faster in game.

But my spawn seems to instantiate all of the array and even more prefabs and then stop indefinitely.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObstacleSpawner : MonoBehaviour
 {
     //For the start of the game a swipe must be done
     private Vector2 swipeStartPos;
     private Vector2 swipeEndPos;
 
 
 
     //array for obstacleprefabs trees
     public GameObject[] trees;
     int puud = 0;
     int maxPuud = 3;
     //second array for enemy prefabs
     public GameObject[] Enemies;
     int vastased = 0;
     int maxVastased = 1;
     public bool mängAlgas = false;
 
     public bool round2 = false;
 
     public GameObject Player;
 
     public int maximumObj;
 
     // Start is called before the first frame update
     void Start()
 
     {
 
        
 
         if (mängAlgas == true)
         {
             
             StartCoroutine(WaitForSecondsAndThenSpawn());
 
 
         }
 
     }
 
   
     void Update()
     {
         
 #if UNITY_EDITOR
         SwipeEnded();
        DetectMousePress();
 #else
         
         DetectTouchPress();
 #endif
 
     }
 
     private void SpawnTrees()
     {
         if (puud <= maxPuud)
         {
             print("I'm Spawning a tree.");
             
             float randomXValue = Random.Range(927f, 1500f);                       
             
             Vector2 spawnPos = new Vector2(randomXValue, 50f);
              
             Instantiate(PickRandomTree(), spawnPos, Quaternion.identity);            
             puud++;
         }
 
     }
     //private void SpawnEnemies()
     //{
     //    if (round2 == true)
     //    {
     //        if (vastased <= maxVastased)
     //        { 
     //        print("yo ma spawnin vastase");
     //            //sündimis vahe x teljel
     //        float randomXValue = Random.Range(927f, 1500f);        
     //            //Sündimis asukoht
     //        Vector2 spawnPos = new Vector2(randomXValue, 6.5f);                
     //            //sünnitamine
     //        Instantiate(PickRandomEnemy(), spawnPos, Quaternion.identity);
     //            vastased++;
     //        }
     //    }
 
     //}
 
     private GameObject PickRandomTree()
     {
         int randomNumber = Random.Range(0, trees.Length);
 
         GameObject treeThatWeChose = trees[randomNumber];
         return treeThatWeChose;
     }
     
     
     //SUVALISE VASTASE VALIMISEKS
     //private GameObject PickRandomEnemy()
     //{
     //    int randomNumber = Random.Range(0, Enemises.Length);
 
     //    GameObject EnemiesThatWeChose = Enemies[randomNumber];
     //    return EnemiesThatWeChose;
     //}
     IEnumerator WaitForSecondsAndThenSpawn()
         {
             yield return new WaitForSeconds(1.5f);
             SpawnTrees();       
             StartCoroutine(WaitForSecondsAndThenSpawn());
         
         }
     
     public void DetectTouchPress()
     {
         foreach (Touch touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began)
             {
                 //start of the touch
                 print("Touch began at" + touch.position);
                 swipeStartPos = touch.position;
             }
             if (touch.phase == TouchPhase.Ended)
             {
                 print("touch ended at" + touch.position);
                 swipeEndPos = touch.position;          
                 
             }
         }
     }
 
 
     //Mouse down - Mouse Up record
     public void DetectMousePress()
     {
         //print("Detection Button Input");
         if (Input.GetMouseButton(0))
         {
             //Start of the press
             //print("Mouse button was pressed at " + Input.mousePosition);
             swipeStartPos = Input.mousePosition;
         }
         if (Input.GetMouseButtonUp(0))
         {
             //End of the press
             //print("Mouse button was released" + Input.mousePosition);
             swipeEndPos = Input.mousePosition;
 
         }
      
     }
     public void SwipeEnded()
     {
         Vector2 swipeVector = swipeEndPos - swipeStartPos;
 
         if (Mathf.Abs(swipeVector.x) > Mathf.Abs(swipeVector.y))
         {
             print("swipe was horizontal");
             if (swipeVector.x < 0f)
             {
                 print("mäng algas");
                 mängAlgas = true;
                 Start();
             }
             
             
         }
    
     }
     
  
 
 }
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

0 Replies

· Add your reply
  • Sort: 

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

203 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 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 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 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 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

NullReference when accessing GameObject in array (C#) 1 Answer

Will a large array of game objects slow my game down? 2 Answers

How would I find the prefab of a gameObject? 2 Answers

Unable to generate a proper engine etc... 0 Answers

Does deactivating a GameObject automatically stop its coroutines? 4 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