Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by alchemist · Mar 05, 2015 at 12:14 AM · instantiatearraysrandom.range

How to instantiate a random object from an array?

I am trying to make a Tetris clone for practice but i cannot get my objects to spawn randomly. I'm still a noob to this any help would be greatly appreciate.

 public int pieceSpawn;
     public GameObject linePiece;
     public Transform spawnLocation;
     
     public GameObject[] pieces;
 
     void Update ()
     {
         pieces = new GameObject[7];
         pieces[0]=GameObject.FindGameObjectWithTag("cube");
         pieces[1]=GameObject.FindGameObjectWithTag("line");
         pieces[2]=GameObject.FindGameObjectWithTag("L");
         pieces[3]=GameObject.FindGameObjectWithTag("J");
         pieces[4]=GameObject.FindGameObjectWithTag("s");
         pieces[5]=GameObject.FindGameObjectWithTag("Z");
         pieces[6]=GameObject.FindGameObjectWithTag("T");
         pieceSpawn = UnityEngine.Random.Range (0,7);
         if (Input.GetKeyDown ("down")) {
 
             Instantiate (pieces[UnityEngine.Random.Range(0,6)], spawnLocation.position, spawnLocation.rotation);

Comment
Add comment · Show 5
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
avatar image NoseKills · Mar 05, 2015 at 12:20 AM 0
Share

Looks pretty good. You don't say what is currently going wrong so it's hard to help. What is happening and how do you want to change it. What do you mean by "spawn randomly" ? The ti$$anonymous$$g, position, rotation ?

avatar image alchemist · Mar 05, 2015 at 12:26 AM 0
Share

Everything is fine until I hit play then i get this: ArgumentException: The prefab you want to instantiate is null

avatar image meat5000 ♦ · Mar 05, 2015 at 12:51 AM 0
Share

$$anonymous$$ake sure you have actually put something inside your gameobject array.

Which part fails? You should be able to use Random.Range without the prefix as UnityEngine should probably be linked as a namespace.

avatar image ThomasVandenberghe · Mar 05, 2015 at 10:51 AM 0
Share

The code indeed looks good, so it means your GameObject.FindGameObjectWithTag couldn't find a gameobject and returned null. Have you tried iterating over the array and logging the gameobjects, it should show some "null" logs.

avatar image d2 · Mar 05, 2015 at 07:53 PM 0
Share

Also you shouldn't use the FindObject method in the Update, ins$$anonymous$$d search your objects in Awake or Start..

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by hexagonius · Mar 05, 2015 at 07:44 PM

I would recommend that you create a prefab for each GameObject (Drag each Gameobject from the Hierarchy somewhere into the Project panel). Then assign all of them to the pieces array in the inspector (Drag them onto the name and they'll add up). This will also reduce the possibility of making a mistake. THEN, you can shorten your code and make it bulletproof:

 void Update ()
   {
      if (Input.GetKeyDown ("down")) {
         Instantiate (pieces[Random.Range(0,pieces.Length)], spawnLocation.position, spawnLocation.rotation);
   }
 }
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
avatar image
0

Answer by iahr · Oct 03, 2016 at 02:10 PM

Get rid of "FindObjectWithTag". It will be always return null because the GameObject you want to "FIND" is not in the "game" already.

Jus use this code

      public Transform spawnLocation;
      
      public GameObject[] pieces;
  
      void Update (){
 
 if (Input.GetKeyDown ("down")) {
  
 Instantiate (pieces[UnityEngine.Random.Range(0,6)], spawnLocation.position, spawnLocation.rotation);
 }

}

In the inspector, put how many GameObject you prefer to choose to instantiate. then Drag the gameobjects in the inspector.

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
avatar image
0

Answer by GunLengend · Oct 03, 2016 at 02:10 PM

Make all of initial in Start()

 void Start()
 {
          pieces = new GameObject[7];
          pieces[0]=GameObject.FindGameObjectWithTag("cube");
          pieces[1]=GameObject.FindGameObjectWithTag("line");
          pieces[2]=GameObject.FindGameObjectWithTag("L");
          pieces[3]=GameObject.FindGameObjectWithTag("J");
          pieces[4]=GameObject.FindGameObjectWithTag("s");
          pieces[5]=GameObject.FindGameObjectWithTag("Z");
          pieces[6]=GameObject.FindGameObjectWithTag("T");
 }
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

10 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

Related Questions

Randomizing a selection from an array 1 Answer

Make a GameObject changes between 2 colors with a time wait 1 Answer

Remove GameObject From Array if it already exists? 1 Answer

Instantiate 1 at a time at each transform in array 1 Answer

How to spawn objects from CSV file 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