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 leooon12 · Mar 23, 2012 at 04:06 AM · javascriptgameobjectarray

Can't destroy Game Object in Array

Hello everyone! Sorry for my bad English :)

I have a map made of tiles. Using my script I generate some transparent tiles, which are located on the same coordinates as usual tiles. I need to destroy transparent tiles, which have no usual tiles under them. But they aren't destroyed. My code:

 #pragma strict
 var tiles : GameObject[,] = new GameObject [32,32];
 var GridPrefab : GameObject;
 var Contact = Array ();
 var x : int;
 var y : int;
 function Start () {
     for (var i = 0; i < y; i++)
         for (var j = 0; j < x; j++) 
             tiles[j,i] = Instantiate (GridPrefab, Vector3 (j*10, 0, i*10), Quaternion.identity);
 }
 function Update () {
     for (var i = 0; i < y; i++)
         for (var j = 0; j < x; j++) 
             Contact = Physics.OverlapSphere (Vector3 (j*10, 0, i*10), 0.1);
             if (Contact.length != 1)
             Destroy (tiles[j,i]);
 }
Comment
Add comment · Show 6
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 farooqaaa · Mar 23, 2012 at 04:10 AM 0
Share

Try using Debug.Log(tiles[j,i].name); ins$$anonymous$$d of Destroy(tiles[j,i]); and see what the output is in the console.

avatar image leooon12 · Mar 23, 2012 at 04:19 AM 0
Share

It writes:

NullReferenceException: Object reference not set to an instance of an object Tile Array.Update () (at Assets/Scripts/Tile Array.js:17)

So... How object reference can be set to its instance?

avatar image farooqaaa · Mar 23, 2012 at 04:22 AM 0
Share

Have you assigned reference to GridPrefab in the Editor?

avatar image leooon12 · Mar 23, 2012 at 04:40 AM 0
Share

Yes, of course. I have a map made of tiles, when I press the play button new tiles made of GridPrefab appear. In some places I have two tiles: one which was placed by me and another generated by script. I want to destroy tiles generated by script in places where they don't have premade twin

avatar image farooqaaa · Mar 23, 2012 at 04:54 AM 0
Share

Are you getting any error or it just doesn't destroy the prefabs?

Show more comments

2 Replies

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

Answer by leooon12 · Mar 23, 2012 at 12:01 PM

That works good! A cup of coffee makes my brain think. I hope it can help someone in future.

 #pragma strict
 var tiles : GameObject[,] = new GameObject[32,32]; //Array for transparent grid
 var GridPrefab : GameObject; //Prefab of transparent gri
 var x : int; //Number of needed tiles
 var y : int;
 //Making a grid made of transparent prefabs
 function Start () {  
     for(var i = 0; i < y; i++)
         for(var j = 0; j < x; j++)
             if(Physics.Raycast (Vector3(j*10, 1, i*10), -Vector3.up, 10)) //Cast a ray, which checks existence of a map tile. If there is no such tile transparent tile won't be generated
                 tiles[j,i] = Instantiate(GridPrefab, Vector3(j*10, 0, i*10), Quaternion.identity);
 }
Comment
Add comment · 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
1

Answer by farooqaaa · Mar 23, 2012 at 04:59 AM

Replace Destroy (tiles[j,i]); with:

 if(tiles[j,i])
     Destroy (tiles[j,i]);

Also replace Contact.length != 1 with Contact.length < 1. Your code should look like this:

 if (Contact.length < 1)
     if(tiles[j,i])
         Destroy (tiles[j,i]);
Comment
Add comment · Show 5 · 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 leooon12 · Mar 23, 2012 at 05:02 AM 0
Share

Now it doesn't get a mistake, but prefabs aren't destroyed. $$anonymous$$aybe I have a mistake in this part?

Contact = Physics.OverlapSphere (Vector3 (j*10, 0, i*10), 0.1);

if (Contact.length != 1)

avatar image farooqaaa · Mar 23, 2012 at 05:05 AM 0
Share

Try if(Contact.length < 1)

avatar image leooon12 · Mar 23, 2012 at 05:11 AM 0
Share

If you use Physics.OverlapSphere (Vector3 (j*10, 0, i*10), 0.1); and there are no other objects around, it will return 1. So I tried if(Contact.length < 2); It didn't work. I have been trying to solve this problem for three hours already =) It seems to me I'm very very bad in scripting

avatar image farooqaaa · Mar 23, 2012 at 05:19 AM 0
Share

It doesn't return 1 when there are no objects around. It returns the number of objects that are around. So in case there there's nothing around it will return 0.

avatar image leooon12 · Mar 23, 2012 at 10:35 AM 0
Share

It is cast from vector, not from object. In fact there is not important 1 or 2 - prefabs must be destroyed with any number in if(Contact.length < 2); because 0 and 1 are less then 2. So it isn't working. Thanks for your help, I'll try to figure out where is the problem hidden

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating from an object that's in an array 1 Answer

Array problem -3 Answers

Find and store gameObjects in a array 2 Answers

Access a String array in one script from another script 0 Answers

NullReferenceException with object 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