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 trriley · Sep 27, 2011 at 08:51 PM · arrayarrays

Instantiate from flexible array

I have a java array () type.
I generate a random number and get an object from that array,
i then declare an object from the array at position (i)
i then removeat (i) in the array for i do not want to initiate the same object twice.
i then instantiate the object.

here is the problem. sometimes the object is declared before the previous one is distorted and i get an object that is not in the array any longer. is there any way to wait for the remove at to finish before i generate the now (i)? Here is my code

var i = Random.Range(0,listWhites.length); var theObject : GameObject = listWhites[i]; listWhites.RemoveAt(i);

var numbers = GameObject.Find("compair");

if (spawned) return; spawned = true;

var newobject = Instantiate (theObject) as GameObject;;

Comment
Add comment · Show 2
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 msknapp · Sep 27, 2011 at 09:43 PM 0
Share

First question, where is "spawned" declared? Is it a local variable or class variable?

Second, what function is containing the code you showed us? The update function? Start function? or is it a function that is called after some event happens? If so, what event.

Third, I don't think I understand "sometimes the object is declared before the previous one is distorted". Did you mean "sometimes the new object is instantiated before it is removed from the array"?

Not sure I know the answer, but I'm sure that the "RemoveAt" function happens immediately, the next line of code cannot execute until it is done. I'm guessing here that the real problem is in some other part of your code. I would take a close look at how that "listWhites" array is created and updated elsewhere.

Have you checked in debug mode that the array indeed does not have any duplicate entries in it to begin with?

http://unity3d.com/support/documentation/$$anonymous$$anual/Debugger.html

avatar image syclamoth · Sep 28, 2011 at 12:05 AM 0
Share

How about using an array shuffling method, and then just instantiating the whole thing in order? Sounds much more straightforward than what you're doing here.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

accessing gameObject script from an array 3 Answers

Save and load arrays into an XML file 1 Answer

Copying a multidimensional array 1 Answer

How do you find one Vector's position of an Array? 1 Answer

Distance error: Cannot cast from source type to destination 0 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