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 MrGobblez · Mar 16, 2013 at 01:32 PM · gameobjectarraydestroymissing

Can't copy Gameobject into array, only refer to an existing one.

Hello, I'm currently trying to make an inventory system where I click on a gameobject, and it's added to inventory and then the clicked object is destroyed. I'm using a GameObject array to store the clicked GameObject, but when the object in the world is destroyed the array spot changes from the GameObject to Missing. I guess this is because the array simply points to the GameObject instead of storing its own copy, how can I instead make a copy for the array, while deleting the original? I've looked around and found no answers or problems similar to this.

Edit: To clarify, the question is solely: How do I copy a GameObject into a GameObject[] instead of just making a reference?

Regards, MrGobblez.

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

Answer by Seth-Bergman · Mar 16, 2013 at 01:47 PM

It would be helpful (in the future) if you post your code..

In this case, it is generally more efficient simply to MOVE your object, than to copy and delete it.. Just change its position, this is simpler AND more efficient

EDIT:

first off, if you are having trouble understanding unity, and you are having trouble accomplishing what you want in your script, how is it irrelevant to post said script?

also, take a moment to consider, why do you think scene changes would increase the number of objects in a scene? I am suggesting a method wherein you are NEVER creating/destroying objects... You are presuming that objects which have been COLLECTED stay intact, but reloading the scene creates a duplicate.. There is no reason why this need be.

but I digress...

I can only guess at the details of your project, but I will assume you have a specific set of objects to collect in the scene. Let me further assume that each object has its own unique name. Taking those two basic assumptions, one way to handle this (just as an example) would be to use an array of strings, and store the name of your object (since presumably you no longer need the actual object using up memory.. Then, whenever you load a different scene, it would include something like:

   function Start(){
  for(listObj in myArrayOfObjects){
  var theObject = GameObject.Find(listObj);
  if(theObject)
  Destroy(theObject);
      }
  }


this will cycle through your array, find the object with the same name if it is there, and destroy it.

when you click the object to collect it, you could say something like:

  var tempArray = myArrayOfObjects;

  myArrayOfObjects = new String[ myArrayOfObjects.length + 1];
  
  for(var i = 0;i < tempArray.length;i++){
  myArrayOfObjects[i] = tempArray[i];
  }
  myArrayOfObjects[tempArray.length] = theNewObject.name; 
  Destroy(theNewObject);

 

this is just an example, hard to say for sure if this is exactly what you want..

to "copy" a GameObject into an array is a deceptive concept, as you have already noted, the items within the array are merely POINTERS to a GameObject. As I already mentioned, if you for some reason prefer to keep an actual copy of the GameObject in the scene, there is ABSOLUTELY NO REASON not to just use the SAME OBJECT. You are proposing DESTROYING IT AND REPLACING IT WITH AN EXACT REPLICA, pointless (unless you're Steven Wright :>) If you copy the object, what does it accomplish?

For the sake of levity, here's how:

   if(clicked){
   myArray[nextSlot] = Instantiate(myClickedObject,somePosition,Quaternion.rotation);
   nextSlot+=1;
   Destroy(myClickedObject);
   }


don't do it that way, it's bad practice, instead why not just

   if(clicked){
   myArray[nextSlot] = myClickedObject;
   nextSlot+=1;
   myClickedObject.transform.position = somePosition;
   }

this accomplishes EXACTLY the same result, without the need for a costly Instantiate

Comment
Add comment · Show 3 · 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 MrGobblez · Mar 16, 2013 at 01:56 PM 0
Share

I didn't post any code as it's working just fine, it's the understanding of Unity that's the problem. I've concidered moving the object, I'm actually doing just that with the inventory screen, I've also "solved" it by disabling collision/mesh, but I think it's gonna be much cleaner to store all objects in an array, because if the player has changed scenes 20 times there will be an unnecessary amount of objects in each scene.

avatar image Seth-Bergman · Mar 16, 2013 at 02:03 PM 0
Share

there is no reason why that would be so..

what I am saying is that you can move the object AND add it to your array, I will give you some sample code, since you haven't provided any, I'll edit my answer shortly..

avatar image MrGobblez · Mar 16, 2013 at 03:14 PM 0
Share

Thanks for clarifying, it makes more sense now. I just got caught up in the whole idea of $$anonymous$$imizing number of objects in the scene.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

If I delete an object stored in an array, will it shorten the array or leave an index position with a null value? 1 Answer

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

Remove an object from an array and destroy it (C#) 2 Answers

how do i create a game object array with gameobjects of multiple tags 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