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 digitalConundrum · Feb 16, 2011 at 09:58 PM · gameobjectinstantiatearraydestroy

using destroy with an array of GameObjects (C#)

The below code is a snippet of a test of what I'm trying to do. I know it isn't working as intended and I'm getting a "Destroying assets is not permitted to prevent data loss error". What I want to do is to have an array of instantiated GameObjects which I can then manipulate by accessing said array. Anyone have any tips on how I could accomplish this? Thanks in advance.

OLD array declaration.... public GameObject[, ,] gemArray = new GameObject[6, 6, 6];

NEW array declaration.... public Object[, ,] gemArray = new GameObject[6, 6, 6];

void Start () { for (int i =0; i < 6; i++) { for (int j =0; j < 6; j++) { for (int k = 0; k < 6; k++) { if ((i != 2 && i != 3) || (j != 2 && j != 3) || (k != 2 && k != 3)) { int num = Random.Range(1, 4);

                     switch (num)
                     {

                         case 1:
                             gemArray[i, j, k] = gem1;

                             Instantiate(gemArray[i, j, k, new Vector3(i, j, k), Quaternion.identity);

                             Destroy (gemArray[i,j,k].gameObject);

                             break;

Okay, after some experimentation on my end, I found that if i declare the array as an Object array instead of a GameObject array the operation

gemArray[i, j, k] =  Instantiate(gem1, new Vector3(i, j, k), Quaternion.identity);

works correctly. However, I now cannot access any of the functions that I would have had access to(namely .tag and .transform). I'm not exactly sure of how to go from here...

Comment
Add comment · Show 5
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 Kona · Feb 16, 2011 at 10:15 PM 0
Share

Do you mean that you want to destroy all gameobjects in the array or the array itself? Have you tried a forloop? :)

avatar image digitalConundrum · Feb 16, 2011 at 11:15 PM 0
Share

$$anonymous$$y intention is to selective destroy elements in the array, and shift new items into those elements. The algorithm is designed, I just made an assumption as to how this would be handled that turned out to be incorrect.

avatar image Jessy · Feb 16, 2011 at 11:31 PM 0
Share

An array completely sucks for what you want to do. Use a List.

avatar image digitalConundrum · Feb 16, 2011 at 11:50 PM 0
Share

"shift" was perhaps an incorrect term to use as I will not actually be doing a linear shift. The order of operations will be: destroy gameobject, raycast to find object that will (physically) shift into that position, shift the transform of the new object, assign the new object to the old object's index, then null the index it originally occupied. I suppose a triply-linked list could do the job, but there would be more operations than I'd like in its execution.

avatar image Eric5h5 · Feb 17, 2011 at 12:39 AM 0
Share

Don't write "solved" in the title, mark the answer as accepted.

1 Reply

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

Answer by digitalConundrum · Feb 17, 2011 at 12:24 AM

Problem solved, an explicit casting cleared up all issues, thanks to all who commented.

solution:

declaration...

public GameObject[, ,] gemArray = new GameObject[6, 6, 6];

casting...

gemArray[i, j, k] = (GameObject)Instantiate(gem1, new Vector3(i, j, k), 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

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

Referencing an instantiated object with a global variable 0 Answers

Destroying 2D Array of Instantated Objects 2 Answers

How can I destroy objects and instantiates new in Array? 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Instantiate from array into array? 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