• 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 /
This question was closed Jun 26, 2014 at 05:54 AM by getyour411 for the following reason:

Duplicate Question

avatar image
0
Question by LeftyTwoGuns · Jun 26, 2014 at 05:42 AM · c#javascriptclasspublicprivate

Field is never assigned to and will always have its default value.

With the help of the smart people here, I have an object pooling script converted from JS to C#. There are some final warnings though preventing the important fields from showing up in the inspector.

Here is the script:

 public class EasyObjectPool : MonoBehaviour {
     private class PoolInfo{
         
         public string poolName;
         public GameObject prefab;
         public int poolSize;
         public bool canGrowPoolSize = true;
         
     }
     
     private class Pool{
         
         private List<PoolObject> list = new List<PoolObject>();
         public bool  canGrowPoolSize;
         
         public void  Add (PoolObject poolObject){
             list.Add(poolObject);
         }
         
         public int Count (){
             return list.Count;
         }
         
         
         public PoolObject ObjectAt ( int index  ){
 
             PoolObject result = null;
             if(index < list.Count) {
                 result = list[index];
             }
             
             return result;
             
         }
     }
     static public EasyObjectPool instance ;
     PoolInfo[] poolInfo;
     
     private Dictionary<string, Pool> poolDictionary  = new Dictionary<string, Pool>();
 
     
     void Start () {
         
         instance = this;
         
         CheckForDuplicatePoolNames();
         
         CreatePools();
         
     }
     
     private void CheckForDuplicatePoolNames() {
         
         for (int index = 0; index < poolInfo.Length; index++) {
             string poolName= poolInfo[index].poolName;
             if(poolName.Length == 0) {
                 Debug.LogError(string.Format("Pool {0} does not have a name!",index));
             }
             for (int internalIndex = index + 1; internalIndex < poolInfo.Length; internalIndex++) {
                 if(poolName == poolInfo[internalIndex].poolName) {
                     Debug.LogError(string.Format("Pool {0} & {1} have the same name. Assign different names.", index, internalIndex));
                 }
             }
         }
     }
     
     private void CreatePools() {
         
         foreach(PoolInfo currentPoolInfo in poolInfo){
             
             Pool pool = new Pool();
             pool.canGrowPoolSize = currentPoolInfo.canGrowPoolSize;
             
             for(int index = 0; index < currentPoolInfo.poolSize; index++) {
                 //instantiate
                 GameObject go = Instantiate(currentPoolInfo.prefab) as GameObject;
                 PoolObject poolObject = go.GetComponent<PoolObject>();
                 if(poolObject == null) {
                     Debug.LogError("Prefab must have PoolObject script attached!: " + currentPoolInfo.poolName);
                 } else {
                     //set state
                     poolObject.ReturnToPool();
                     //add to pool
                     pool.Add(poolObject);
                 }
             }
             
             Debug.Log("Adding pool for: " + currentPoolInfo.poolName);
             poolDictionary[currentPoolInfo.poolName] = pool;
             
         }
     }
     
     public PoolObject GetObjectFromPool ( string poolName  ){
         PoolObject poolObject = null;
         
         if(poolDictionary.ContainsKey(poolName)) {
             Pool pool = poolDictionary[poolName];
             
             //get the available object
             for (int index = 0; index < pool.Count(); index++) {
                 PoolObject currentObject = pool.ObjectAt(index);
                 
                 if(currentObject.AvailableForReuse()) {
                     //found an available object in pool
                     poolObject = currentObject;
                     break;
                 }
             }
             
             
             if(poolObject == null) {
                 if(pool.canGrowPoolSize) {
                     Debug.Log("Increasing pool size by 1: " + poolName);
                     
                     foreach (PoolInfo currentPoolInfo in poolInfo) {    
                         
                         if(poolName == currentPoolInfo.poolName) {
                             
                             GameObject go = Instantiate(currentPoolInfo.prefab) as GameObject;
                             poolObject = go.GetComponent<PoolObject>();
                             //set state
                             poolObject.ReturnToPool();
                             //add to pool
                             pool.Add(poolObject);
                             
                             break;
                             
                         }
                     }
                 } else {
                     Debug.LogWarning("No object available in pool. Consider setting canGrowPoolSize to true.: " + poolName);
                 }
             }
             
         } else {
             Debug.LogError("Invalid pool name specified: " + poolName);
         }
         
         return poolObject;
     }
     
 }

For my public variables, I get the warning posted in the question topic: Field is never assigned to and will always have its default value. Changing the PoolInfo class from private to public fixes those warnings but the JS script has this class as private class PoolInfo extends System.Object {. Does JS treat private and public classes differently than C#?

For line 37 I get the warning: "Field 'EasyObjectPool.poolInfo' is never assigned to, and will always have its default value 'null'" and I don't know how to fix that. Any advice?

Comment
Add comment · Show 1
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 getyour411 · Jun 26, 2014 at 05:54 AM 0
Share

Duplicate of http://answers.unity3d.com/questions/735311/field-is-never-assigned-to-and-will-always-have-it-1.html

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Field is never assigned to, and will always have its default value 3 Answers

Creation of RPG Class Stat Presets? 1 Answer

Distribute terrain in zones 3 Answers

Why can't i access my c# class from javascript 1 Answer

CSharp Classes = Scripts? Difference between private string and normal string? 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