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 LeftyTwoGuns · Jun 19, 2014 at 03:05 AM · c#javascriptconvert

JS to C# conversion is a complete disaster

I've spent literally all day trying to convert a JS script for an object recycler to C#. I know absolutely nothing about JS so I had to do a lot of Googling and documentation searching. But I think it's all the for loops that done me in because they're throwing a whole list of errors at me. Can anyone try to tell what I did wrong? Here is the original JS script:

 #pragma strict
 import System.Collections.Generic;
 
 
 private class PoolInfo extends System.Object {
     public var poolName : String;
     public var prefab : GameObject;
     public var poolSize : int;
     public var canGrowPoolSize : boolean = true;
 }
 
 private class Pool extends System.Object {
     private var list : List.<PoolObject> = new List.<PoolObject>();
     public var canGrowPoolSize : boolean;
     
     public function Add(poolObject : PoolObject) {
         list.Add(poolObject);
     }
     
     public function Count() : int {
         return list.Count;
     }
     
     public function ObjectAt(index : int) : PoolObject {
         var result : PoolObject;
         if(index < list.Count) {
             result = list[index];
         }
         
         return result;
     }
 }
 
 static var instance : EasyObjectPool;
 var poolInfo : PoolInfo[];
 
 private var poolDictionary : Dictionary.<String, Pool> = new Dictionary.<String, Pool>();
 
 function Start () {
     //set instance
     instance = this;
     //check for duplicate names
     CheckForDuplicatePoolNames();
     //create pools
     CreatePools();
 }
 
 private function CheckForDuplicatePoolNames() {
     for (var index : int = 0; index < poolInfo.Length; index++) {
         var poolName = poolInfo[index].poolName;
         if(poolName.Length == 0) {
             Debug.LogError(String.Format("Pool {0} does not have a name!",index));
         }
         for (var internalIndex : int = 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 function CreatePools() {
     for (var currentPoolInfo in poolInfo) {
         
         var pool : Pool = new Pool();
         pool.canGrowPoolSize = currentPoolInfo.canGrowPoolSize;
         
         for(var index : int = 0; index < currentPoolInfo.poolSize; index++) {
             //instantiate
             var go : GameObject = Instantiate(currentPoolInfo.prefab);
             var 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;
     }
 }
 
 
 function GetObjectFromPool(poolName : String) : PoolObject {
     var poolObject : PoolObject;
     
     if(poolDictionary.ContainsKey(poolName)) {
         var pool : Pool = poolDictionary[poolName];
         
         //get the available object
         for (var index : int = 0; index < pool.Count(); index++) {
             var currentObject : PoolObject = pool.ObjectAt(index);
             
             if(currentObject.IsAvailableForReuse()) {
                 //found an available object in pool
                 poolObject = currentObject;
                 break;
             }
         }
         
         if(poolObject == null) {
             if(pool.canGrowPoolSize) {
                 Debug.Log("Increasing pool size by 1: " + poolName);
                 
                 //find the prefab for this poolname
                 for (var currentPoolInfo in poolInfo) {    
                     if(poolName == currentPoolInfo.poolName) {
                         var go : GameObject = Instantiate(currentPoolInfo.prefab);
                         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;
 }

Here is my hack-job in C#:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 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 GameObject ObjectAt ( int index  ){
 
         PoolObject result;
         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++) {
             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(int currenPoolInfo in poolInfo){
 
             Pool pool = new Pool();
             pool.canGrowPoolSize = currentPoolInfo.canGrowPoolSize;
             
             for(int index = 0; index < currentPoolInfo.poolSize; index++) {
                 //instantiate
                 GameObject go = Instantiate(currentPoolInfo.prefab);
                 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 GameObject GetObjectFromPool ( string poolName  ){
             PoolObject poolObject;
             
             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.IsAvailableForReuse()) {
                         //found an available object in pool
                         poolObject = currentObject;
                         break;
                     }
                 }
 
 
             if(poolObject == null) {
                 if(pool.canGrowPoolSize) {
                     Debug.Log("Increasing pool size by 1: " + poolName);
 
                     foreach (int currentPoolInfo in poolInfo) {    
 
                         if(poolName == currentPoolInfo.poolName) {
 
                             GameObject go = Instantiate(currentPoolInfo.prefab);
                             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;
     }
 }
 
 
 
 





Comment
Add comment · Show 2
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 vexe · Jun 19, 2014 at 03:09 AM 1
Share

Would be helpful if you posted the errors and in which lines they occurred in the code you posted 'here' (not the original - the line numbering will differ) so we can quickly identify the problem ins$$anonymous$$d of having us to analyze/compare the two snippets and figure things out for your.

avatar image LeftyTwoGuns · Jun 19, 2014 at 04:09 AM 0
Share

Well, I'm showing 12 different errors so that would be quite a bit of information.

Lines 62 and 63 and 67- The name 'poolName' does not exist in the current context

Line 64 and 68- The name 'String' does not exist in the current context

Line 76 and 123- Cannot convert type 'EasyObjectPool.PoolInfo' to 'int'

Line 109- Cannot implicitly convert type 'UnityEngine.GameObject' to 'PoolObject'

Line 111- Type 'PoolObject' does not contain a definition for 'IsAvailableForReuse' and no extension method 'IsAvailableForReuse' of type 'PoolObject' could be found

Line 119 and 147- Use of unassigned local variable 'poolObject'

Line 147- Cannot implicitly convert type 'PoolObject' to 'UnityEngine.GameObject'

Basically, I've misunderstood some important things about my for loops

1 Reply

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

Answer by robertbu · Jun 19, 2014 at 04:03 AM

Here is a quick pass on the code. You did not provide the PoolObject class, so I had to do a bit of guessing. You'll need to compare this code with your original...there were a fair number of changes. Note I did not compare your translation to the original Javascript...I just went through and fixed compiler errors so that most if not all of the errors should be resolved.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 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 GameObject ObjectAt ( int index  ){
             
             PoolObject result;
             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 GameObject 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;
     }
 
 }
Comment
Add comment · Show 9 · 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 LeftyTwoGuns · Jun 19, 2014 at 04:26 AM 0
Share

This is beautiful, thank you. I knew I was doing the for loops wrong.

I am getting two errors, though

Line 45- The type or namespace name 'Dictionary`2' could not be found. Are you missing a using directive or an assembly reference?

Line 18- The type or namespace name 'List`1' could not be found. Are you missing a using directive or an assembly reference?

PoolObject is the name of the second script that works with this one, which I didn't have any problems converting. Are these two errors co$$anonymous$$g from this script not being able to access the other one?

avatar image vexe · Jun 19, 2014 at 04:58 AM 1
Share

You sure you have a using System.Collections.Generic; at the top?

avatar image robertbu · Jun 19, 2014 at 05:08 AM 0
Share

Are these two errors co$$anonymous$$g from this script not being able to access the other one?

I wouldn't think so. These two lines look fine. If you will post the PoolObject class, I'll see if I have any trouble.

avatar image LeftyTwoGuns · Jun 19, 2014 at 07:31 AM 0
Share

Actually, I had forgot to reinsert System.Collections.Generic but strangely it added some more errors to the script.

Here is the PoolObject script as I converted it to C#. It doesn't give any errors but like I said earlier, I known virutally nothing of JS syntax so not sure if I got it right (really just that last line I'm kind of unsure about):

 public class PoolObject : $$anonymous$$onoBehaviour {
 
     [HideInInspector]
     public bool availableForReuse = true;
 
 
     void Activate () {
 
         availableForReuse = false;
         gameObject.SetActive(true);
     
     }
     
 
     void ReturnToPool () {
 
         gameObject.SetActive(false);
         availableForReuse = true;
 
     
     }
 
     public bool AvailableForReuse () {
         //true when isAvailableForReuse & inactive in hierarchy
 
         return availableForReuse && (gameObject.activeInHierarchy == false);
 
 
 
     }
 }
 
avatar image vexe · Jun 19, 2014 at 11:00 AM 0
Share

Don't see anything wrong with the last snippet. What errors you're getting?

Show more comments

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

22 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Best place to learn Marching Cubes Algorithm? 2 Answers

Which language should I start learning for scripting? 1 Answer

Calling c# function from a JS 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