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 cacysunlee · Mar 03, 2011 at 12:56 PM · gameobjectarrayrandomnull

how to check for GameObject is null in array with random

how to check for GameObject is null in array with random ?

unity says : "The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."

my try with WHILE don't proceed

var ArrayRandomPlace = new Array();

     ArrayRandomPlace.Add(empty01);
     ArrayRandomPlace.Add(empty02);  
     ArrayRandomPlace.Add(empty03);


     while (ArrayRandomPlace == 0) return ;    
     Instantiate(card01, ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)].transform.position, transform.rotation); yield WaitForSeconds(0.1);
     Destroy(ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)]);       
     Instantiate(card02, ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)].transform.position, transform.rotation); yield WaitForSeconds(0.1);
     Destroy(ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)]); 
     Instantiate(card03, ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)].transform.position, transform.rotation); yield WaitForSeconds(0.1);
     Destroy(ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)]); 

Edit : here you see my next try, complete code :

// my prefabs cards var card01 : GameObject; var card02 : GameObject; var card03 : GameObject; var card04 : GameObject; var card05 : GameObject; var card06 : GameObject; var card07 : GameObject; var card08 : GameObject; var card09 : GameObject; var card10 : GameObject; var card11 : GameObject; var card12 : GameObject; var card13 : GameObject; var card14 : GameObject; var card15 : GameObject; var card16 : GameObject;

// my empty container positions in scene X, Y, Z var empty01 : GameObject; // 0,0,0 var empty02 : GameObject; // 2,0,0 var empty03 : GameObject; // 4,0,0 var empty04 : GameObject; // 6,0,0 var empty05 : GameObject; // 0,-2,0 var empty06 : GameObject; // 2,-2,0 var empty07 : GameObject; // 4,-2,0 var empty08 : GameObject; // 6,-2,0 var empty09 : GameObject; // 0,-4,0 var empty10 : GameObject; // 2,-4,0 var empty11 : GameObject; // 4,-4,0 var empty12 : GameObject; // 6,-4,0 var empty13 : GameObject; // 0,-6,0 var empty14 : GameObject; // 2,-6,0 var empty15 : GameObject; // 4,-6,0 var empty16 : GameObject; // 6,-6,0

function newField() {
var ArrayRandomPlace = new Array();

         ArrayRandomPlace.Add(empty01);
         ArrayRandomPlace.Add(empty02);  
         ArrayRandomPlace.Add(empty03);
         ArrayRandomPlace.Add(empty04);
         ArrayRandomPlace.Add(empty05);
         ArrayRandomPlace.Add(empty06);
         ArrayRandomPlace.Add(empty07);  
         ArrayRandomPlace.Add(empty08);
         ArrayRandomPlace.Add(empty09);
         ArrayRandomPlace.Add(empty10);
         ArrayRandomPlace.Add(empty11);
         ArrayRandomPlace.Add(empty12);  
         ArrayRandomPlace.Add(empty13);
         ArrayRandomPlace.Add(empty14);
         ArrayRandomPlace.Add(empty15);
         ArrayRandomPlace.Add(empty16);       

         var rndIndex = Random.Range(0, ArrayRandomPlace.length - 1);
         var rndPlace = ArrayRandomPlace[rndIndex];

         Instantiate(card01, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card02, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card03, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card04, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card05, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card06, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card07, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card08, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card09, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card10, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card11, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card12, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card13, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card14, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card15, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
         ArrayRandomPlace.RemoveAt(rndIndex); 
         Instantiate(card16, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);

}

here is a screenshot to this : http://i55.tinypic.com/2zhlaja.jpg

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
1
Best Answer

Answer by Statement · Mar 03, 2011 at 01:05 PM

while (ArrayRandomPlace == 0) return ;  

That is just odd code right there. You're checking if an array is 0? While it is so, it would return?

By the way you need to make the random index one less than length because Random.Range is inclusive. Make it Random.Range(0, ArrayRandomPlace.length - 1). The reason you're getting errors is because you're randomly destroying items, and can try do destroy the object twice, or you might destroy an item that you then want to instantiate with the position from the destroyed object. You should remove the object from the array to stop this from happening, or testing if the returned object is null or not.

You should define what you are trying to achieve because that code looks so strange to me.

Basically your code reads to me; "Create a card, at a random location. Then destroy a random location." Did you mean; "Create a card at a random location. Then destroy the card."?


You wrote: "Create a card to random position of empty in scene, then destroy empty, so that the next card do not use the position of last used empty"

So, then we can just remove the object from the list, not remove the object itself :)
Now I think I get it.

// 1. Select a random place. var rndIndex = Random.Range(0, ArrayRandomPlace.length - 1); var rndPlace = ArrayRandomPlace[rndIndex];

// 2. Create card01 at that place. Instantiate(card01, rndPlace.transform.position, transform.rotation);

// 3. Wait 100 milliseconds. yield WaitForSeconds(0.1);

// 4. Remove the place from the list, so it won't show up again. ArrayRandomPlace.RemoveAt(rndIndex);

// 5. (Optionally you might want to destroy the place as well) // Destroy(rndPlace);

If this code is repeated several times, then there won't be any fear of using different places within the call, and same places across different calls because we store the random item so it doesn't change until the next time, and we also remove the object from the list so it's impossible for it to show up again (unless you added it twice etc).

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 cacysunlee · Mar 04, 2011 at 07:18 AM 0
Share

i mean "Create a card to random position of empty in scene, then destroy empty, so that the next card do not use the position of last used empty".

thanks for your suggestions. but i don't know how to realize this

avatar image Statement · Mar 04, 2011 at 11:05 AM 0
Share

See my update, hope it helps.

avatar image cacysunlee · Mar 07, 2011 at 01:58 PM 0
Share

thank you for you help, i edited my first post and see the new full code. now all cards use the same empty position, and this code do not placed all cards, unity says : "ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count."

see also screenshot

avatar image Statement · Mar 07, 2011 at 05:26 PM 0
Share

Well, you're deleting an item from the array, then trying to delete the same index over and over again. Eventually, there won't be such index (since you're removing items with the same index over and over).

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

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

Array element coming as not null 2 Answers

How to access gameobject array set in inspector? 2 Answers

Simple Looping drag and drop game 1 Answer

how to use GameObject only once from array with random 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