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 21, 2020 at 11:27 AM · gameobjectprefabarray

How can I instantiate a single prefab from an array?

I have a code that spawns obstacles in front of the player. It has an array of obstacle prefabs, and chooses 1 random obstacle from that array. I have made a count int into to the game that look how many prefabs can be instantiated. I also put a destroy with tag if the count is crossed. But now it just instantiates bunch of obstacles and deletes them "1 second" later.

 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;

     public bool mängAlgas = false;    //game began bool
     public bool round2 = false;
     public GameObject Player;    
     public int maximumObj;    
     public GameObject[] howManyTreesHaveBeenInstantiated;    
     public AudioSource mainTheme;        
     private int count = 0;
     public AudioSource jumpSound;          
     public GameObject surmaLaulja;
     public GameObject ScoreCounter;
 
     // Start is called before the first frame update
     void Start()    
     {
         surmaLaulja.SetActive(false);
 
         if (mängAlgas != true)
         {
             return;
         }
 
         else if (mängAlgas == true)
         {
             mainTheme.Play();
             StartCoroutine("SpawnDelay");
             //return;
         }            
     } 
     void Update()
     {
         SpawnDelay();
         howManyTreesHaveBeenInstantiated = GameObject.FindGameObjectsWithTag("Tree");
         count = howManyTreesHaveBeenInstantiated.Length;
         if (ScoreCounter.GetComponent<ScoreCounter>().mängLäbi == true)
         {
             surmaLaulja.SetActive(true); 
         }
 
 #if UNITY_EDITOR
        DetectMousePress(); 
        
        
 #else
        
        DetectTouchPress();
        
 #endif
         SwipeEnded();
     }
 
     private void SpawnTrees()
     {
         if (count <= 1)
         {
             print("I'm Spawning a tree.");
             
             float randomXValue = Random.Range(927f, 1500f);                       
             
             Vector2 spawnPos = new Vector2(randomXValue, 50f);
              
             Instantiate(PickRandomTree(), spawnPos, Quaternion.identity);                          
         }
         else if ( >= count)
         {
             Destroy(GameObject.FindGameObjectWithTag("Tree"));
         }
     }
     private GameObject PickRandomTree()
     {
         int randomNumber = Random.Range(0, trees.Length);
 
         GameObject treeThatWeChose = trees[randomNumber];
         return treeThatWeChose;
     }                
     public void DetectTouchPress()
     {
         foreach (Touch touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began)
             {
                 jumpSound.Play();
                 //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))
         {
             jumpSound.Play();
             //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

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

Creating an array of prefabs? 4 Answers

Load Prefabs in Array with Javascript 1 Answer

Prefab not loading in data from Inspector 1 Answer

Array of Arrays of GameObjects/Prefabs (C#) 1 Answer

Get Script from gameobject knowing only parent script 1 Answer


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