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
0
Question by vincentamato · Dec 07, 2014 at 11:16 PM · gameobjectrandominstantiationinstantiate-game-object

Randomly instantiating a list of objects at a random position.

Hi. I recently tried asking this question but I realized it was not a sufficient question. In my game the player is climbing up an infinite wall. I would like objects to fall and try to nock the player off. I have five objects that I want to fall. I want my game to randomly pick one and instantiate it between two points. I would also like the rotation to be random. Here is what I have so far:

 using UnityEngine;
 using System.Collections;
  
  public class ObjectSpawner : MonoBehaviour
      {
              public GameObject[] obj = new GameObject[5];
          
              // Use this for initialization
              void Start ()
              {
                      SpawnObject ();
              }
          
              // Update is called once per frame
              void Update ()
              {
          
              }
      
              //Spawns objects
              void SpawnObject ()
              {
                  Instantiate (obj (Random.Range (0, 4)), transform.position, Random.rotation);
              }
      }

I got a few errors in this code and I was wondering if someone could point me in the right direction. For example some of these errors are: The member ObjectSpawner.obj' cannot be used as method or delegate. Another is: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments. Another is: Argument #1' cannot convert object' expression to type `UnityEngine.Object'. Please help. Thanks!

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Dec 07, 2014 at 11:17 PM 0
Share

$$anonymous$$aybe edit your post and describe what the errors are.

1 Reply

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

Answer by static_cast · Dec 07, 2014 at 11:33 PM

Use Instantiate(obj[Random.Range (0, 4)], transform.position, Random.rotation);

The problem was the fact that you use [] instead of () to reference the location in an array.

You can also do new Vector3(Random.Range (0, 4),Random.Range (0, 4),Random.Range (0, 4)) instead of transform.position in the Instantiate call. This will instantiate an object at any position with 0,0,0 and 4,4,4. ;)

Comment
Add comment · Show 4 · 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 MrSoad · Dec 07, 2014 at 11:35 PM 0
Share

Well spotted(+1) :)

avatar image static_cast · Dec 07, 2014 at 11:42 PM 0
Share

Thank you, sir. :)

avatar image vincentamato · Dec 07, 2014 at 11:59 PM 0
Share

@static_cast Thank you so much bro!!!! It worked!!!!

avatar image static_cast · Dec 08, 2014 at 12:01 AM 0
Share

No problem. :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# GameObjects Instantiate Into Each Other Issue 1 Answer

GameObject variable change for all of the objects 1 Answer

Three Spots For Three Random Objects 1 Answer

I want gameobjects to move randomly on screen without overlapping.` 2 Answers

How to randomly spawn three non repeating gameobjects from an array? 2 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