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 /
  • Help Room /
avatar image
0
Question by _watcher_ · May 04, 2017 at 09:51 AM · arraynullremovepointerbuiltin array

ObjectPool for Class Instances using Built-In Array

Hi there,

I have super simple problem i'm struggling with.

Im trying to reuse custom MyClass instances, so i've built an Object pool for them. Basically, when a reference to an instance is no longer needed on stage, its moved into the pool.

To move the reference (pointer), i point it to an ObjectPool.myArray[i], and (if using a List as the on-stage collection type), i use MyList.RemoveAt to remove the list item from that List.

This works, as RemoveAt doesnt nullify the MyClass instance, only removes its pointer (which prior to that, is assigned into ObjectPool's myArray[i]).

That works, however i want to use built-in array for on-stage display, not a List.

QUESTION

How to remove pointer to an item from built-in Array, without nullifying the value?

All i know is possible with built-in arrays is this:

array[i] = null;

But that destroys the instance. I only want to (re)move the pointer.

Another approach i can think of is: assigning a copy of the built in array to the builtin array "with the item in question nullified" - did not test this yet, but if this works, it seems expensive and seems to defeat the purpose of using builtin array in the first place ( i like my fixed-size, indexed, speedy collection type) - but i could just use List.removeAt / List.Insert instead.. (not a fan)

Thoughts please

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 _watcher_ · May 04, 2017 at 11:44 AM

I've made some tests, and realized my own ignorance! Apparently what i wrote above is NOT correct.

Truth is:

Nullifying built-in array item's value doesn't destroy the instance, only removes the pointer.

Some examples you can run yourself:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class OneTimeSimpleTests : MonoBehaviour
 {
     public class MyClass
     {
         // gameobj reference
         public GameObject gameObject;
 
         // array of strings reference
         public string[] strArray;
     }
 
     private MyClass[] classArr1;
     private MyClass[] classArr2;
 
     void Start () {
 
         /// INIT
         /// 
 
         classArr1 = new MyClass[10];
         classArr2 = new MyClass[10];
 
         // fill in the arrays
         for (int i = 0; i < 10; i++)
         {
             // class instances must be manually instantiated 
             classArr1[i] = new MyClass();
             classArr2[i] = new MyClass();
             classArr1[i].strArray = new string[2] { "five", "six" };
             classArr2[i].strArray = new string[2] { "seven", "eight" };
         }
 
         // TESTS
         //
 
         MyClass inst = classArr1[0];
         classArr1[0] = null;
         Debug.Log(inst); // MyClass // HOHOOO!
         
         
         classArr1[0] = classArr2[0];
         classArr2[0] = null;
         Debug.Log(classArr1[0]); // MyClass // HOHOOO!
         
         
         classArr2[0].gameObject = gameObject;
         classArr1[0].gameObject = classArr2[0].gameObject;
         classArr2[0].gameObject = null;
         Debug.Log(classArr1[0].gameObject); // GameObject // HOHOOO!
         
         
         classArr2[0].gameObject = gameObject;
         classArr1[0].gameObject = classArr2[0].gameObject;
         DestroyImmediate(classArr2[0].gameObject);
         Debug.Log(classArr1[0].gameObject); // null
     }
 }

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

107 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is my 2D array outputting null? (all except one spot) 1 Answer

Why do Constructors return null when used within EditorWindow 1 Answer

How remove/delete/destroy an element from an array by value in c#? 2 Answers

[Solved] Array is empty after Scene change.. 1 Answer

Array - Difference between .Clear() and Null? 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