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
1
Question by schwertfisch · Jan 09, 2011 at 06:34 PM · arrayrandomspawn

How do I Spawn a random gameObject from Array1, at a random position of gameObjects from Array2 ?

My game is a board game (chess-like). The squares of the board that are empty, change during play, as my main object moves and eat various collectibles.

I have a script, that will spawn one random collectible in a random empty square, everytime I (manipulating my main object) eat a collectible.

This script will do it, using 2 Arrays:

1) the first Array, is this of the empty squares. It is actually an array of game objects. These game objects are empty game objects with a box collider, each of which sit on each one of the boards squares. Their tags change (full <-> empty) depending on whether there is a game object (my main game object or a collectible) on them, so this first Array updates each time I move my game object and is redifined, by the empty game objects with the empty tag.

2) the second Array is fixed. It is comprised of the collectibles that will randomly get spawned at one of the empty board squares, each time I eat a collectible.

How would I spawn a random game object from the 2nd Array, at one random position of the 1st Arrays empty game objects (that actually represent the empty squares of the board)?

So, Instantiate (WHAT, WHERE); ?

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

1 Reply

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

Answer by Loius · Jan 09, 2011 at 06:56 PM

var objectsToSpawn : Object[]; var spawnLocations : Transform[]; var spawnOffset : Vector3; // where to spawn the objectsw relative to the grid tiles

function SpawnOne() : GameObject { var thingToSpawn : int = Random.Range( 0, objectsToSpawn.length ); var placeToSpawn : int = Random.Range( 0, spawnLocations.length ); var newObject : GameObject = Instantiate( objectsToSpawn[ thingToSpawn ], spawnLocations[placeToSpawn].position + spawnOffset, Quaternion.identity ); return newObject; }

Comment
Add comment · Show 6 · 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 schwertfisch · Jan 09, 2011 at 07:59 PM 0
Share

Thanks Vicenti, I'll try that now.

What is the use of "return newObject"?

avatar image schwertfisch · Jan 09, 2011 at 08:32 PM 0
Share

It works. The "spawnOffset" was a nice touch. I didn't intend on using it at the beginning but it ended up really useful. I really appreciate your help :)

avatar image schwertfisch · Jan 09, 2011 at 08:37 PM 0
Share

While it works, there is a problem: The newObjects are spawned as clones and their colliders don't trigger a tag change to the empty game objects that are located on each board square. Thus, the engine doesn't see the change in the empty positions of the board and the spawning can take place on occupied squares too. Does it have to do with the newObjects being "Clones"?

avatar image Loius · Jan 10, 2011 at 01:32 AM 0
Share

Everything is always spawned as a clone. :) That doesn't interefere with collision; whatever is buggy about collision is due to how you're handling it in the collided object. If you're checking "if .name == blah" then yeah, having Clone on the end will mess that up - but that's why SpawnOne returns the new object. You can change its name right after calling SpawnOne - var x = SpawnOne(); x.name = "BetterName"; or whatever. :)

avatar image schwertfisch · Jan 10, 2011 at 06:52 PM 0
Share

Thanks Vicenti. I'm actually checking for the tag. I don't want to get into it in this question as I made a seperate question about the issue ("Instantiated (clone of) prefab, doesnt trigger another objects collider when inside it.") and I'm at the moment trying hard to figure it out. Your help was very big on this though, thanks again!

Show more comments

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

No one has followed this question yet.

Related Questions

Simple array an spawning question 6 Answers

Random textures... 1 Answer

Calling random functions 4 Answers

Random select from array and spawn 1 Answer

How can i delete an item from an array after being used? (So, it won't be repeated) 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