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 krimzondestiny · Dec 18, 2011 at 03:55 AM · instantiatearrayrandomtagtags

instantiate a random object from multiples via 'tag'

I have been researching and trying to figure this one out to no avail. I have 4 different objects with the tag "PlayerPawn". I want to instantiate a random object from the 'FindGameObjectWithTag' list/array. I'm not sure how to accomplish this. I'm thinking:

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 krimzondestiny · Dec 18, 2011 at 03:27 AM 0
Share

cont'd from first post. I want to run a loop that instantiates a new random prefab each time the loop fires. I would put my code up, but each time I do, I get what I think is an epiphany, but it never works. LOL. Thanks in advance!

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by aldonaletto · Dec 18, 2011 at 04:28 AM

The question isn't totally clear, but I suppose you want to get an array of PlayerPawn tagged objects (prefabs or scene objects) and clone a randomly selected element, like this:

  ...
  var objects = GameObject.FindGameObjectsWithTag("PlayerPawn");
  var newObj = Instantiate(objects[Random.Range(0, objects.length)], pos, rot);
  ...
But creating an array of PlayerPawn tagged objects with FindGameObjectsWithTag isn't a good idea. This function only finds scene objects, not prefabs. You may, of course, place the 4 different PlayerPawn objects in your scene in the Editor, and use FindGameObjectsWithTag to fill an array with them - but do it only in Start; if you use FindGameObjectsWithTag during program execution, the other objects already instantiated will enter the list as well. This will result in one of them being more and more frequent than the others - the more the object is instantiated, the more it will appear in the array, increasing its probability in being instantiated again - not to mention that it will terribly slow down your game.

A better solution would be to create a public array and assign the PlayerPawn prefabs to it in the Inspector:

var objects: GameObject[]; // drag the prefabs here at the Editor
  ...
  var newObj = Instantiate(objects[Random.range(0, objects.length)], pos, rot);
Comment
Add comment · Show 1 · 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 krimzondestiny · Dec 18, 2011 at 12:31 PM 0
Share

Yeah, that was the first problem. I figured out that 'FindGameObjectsWithTag' only works on objects that are already in the scene after I posted this. I'm trying to Instantiate a prefab, so it won't work because it doesn't exist yet, LOL.

I think your second solution makes sense to me, I'm going to try it once I get home.

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

6 People are following this question.

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

Related Questions

multiple object in multiple spawn Point with out repeat 0 Answers

Instantiate prefab with different tag 4 Answers

How to instantiate the first 8 gameobjects in an array (UnityScript)? 1 Answer

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

Spawning Objects Using An Array. 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