Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Quick Question

Discussion in 'Scripting' started by RoughSpaghetti3211, Mar 6, 2017.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Hello , need some help

    I have an empty GameObject that has my ObjPool script attach. At run time I want to reference it into my ObjPoolManager that has the variable type ObjPool objPool. For the life of me I cant get it to work so deleted and came here, please help

    Basically want to script the drag and drop GameObject onto ObjPoolManager.objPool

    Thanks
     
    Last edited: Mar 6, 2017
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    If your ObjPoolManager is in the scene(I assume it is) add a

    public ObjPool objpool; variable and just drag and drop onto the variable slot.

    What issue are you encountering that keeps it from working?
     
  3. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Make sure that the objPool variable in your ObjPoolManager is marked public.
     
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Still no go, but im not sure on the Syntax since im referencing in class
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    public ObjPool objpool;

    This should be in your ObjPoolManager. Then you should see the slot in the inspector. Drag and drop into it.
     
  6. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Problem is i need to change the reference to several different GameObject with ObjPool script attach. Basically need to pool several types of objects

    Instead of creating a array public ObjPool[] objpool I want to set the reference to a different gameObject. with ObjPool script attached

    This generates the game environment so I don't want to hold onto a large array after load
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    I'm not sure what you mean. What is ObjPool? You have several of these in your scene?

    Having an array in your manager where you drag and drop several ObjPool isn't really going to hurt anything.

    You could create an array of gameobjects if you wanted and use getComponent and loop through those gameobjects, but really it's not going to make a difference and using getComponent will be slower than doing a array of the ObjPool straight up.
     
  8. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Can you post the two scripts? ObjPool and ObjPoolManager?
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  10. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    IM not in front of the code this second but let me try clarify. I create an empty gameObject (goTree) and attach the ObjPool Script below ..pseudocode code


    Code (CSharp):
    1. public class ObjPool : MonoBehaviour {
    2.  
    3.     public GameObject objForPooling;   // this is connected in scene to a prefab I want the manager to pool at runtime
    4.    
    5.  
    6.     //
    7.     // CODE FOR POOLING
    8.     public GameObject nextPoolObj {
    9.         get{}
    10.         set{}
    11.         //return object to use from pool
    12.     }
    13.  
    14. }
    15.  
    16.  
    17.  
    18.  
    19.  
    20. public class ObjPoolManager : MonoBehaviour {
    21.  
    22.     public ObjPool objPool;    // This is what need to point to goTree at runtime    
    23.  
    24.  
    25.     //
    26.     // Set objPool to the desired GameOject with ObjPool attache eg. goTree here
    27.  
    28.         ????????????????
    29.  
    30.     //
    31.     // Loop over number of tree and position
    32.     for(int i = 0; i < numTrees; i++){
    33.         GameObject goTree = objPool.nextPoolObj;
    34.         //
    35.         // Do stuff
    36.     }
    37.  
    38. }
    39.  
     
    Last edited: Mar 6, 2017
  11. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Basically I need to know how to set public ObjPool objPool in the ObjPoolManager to a specified GameObject that contains a ObjPool script... ObjPoolManager expects a type of ObjPool .. connecting by hand works I just can seem to get it working by scripting it
     
  12. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    Click-dragging works because Unity can detect the ObjPool component on it.

    You just have to find the correct GameObjects in the scene (the ones with ObjPool components).

    Code (CSharp):
    1. public ObjPool[] pool;
    2.  
    3. void Start()
    4. {
    5.     pool = FindObjectsOfType<ObjPool>();
    6. }
     
  13. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695

    Yup that is what I was after, thanks heaps. Just out of curiosity, I drafted up this junk below. Will it work? Just for
    my own understanding. Im new to unity scripting and C#

    Code (CSharp):
    1.  
    2. public GameObject objForPooling;
    3. public ObjPool objPool;
    4.  
    5. void Start() {
    6.  
    7.     foreach (string name in goNames){
    8.  
    9.         objForPooling = GameObject.Find(name);
    10.         ObjPool objPool = objForPooling.GetComponent( typeof(ObjPool) ) as ObjPool;
    11.  
    12.         //
    13.         // Pool
    14.         for(int i = 0; i < numTrees; i++){
    15.             GameObject goTree = objPool.nextPoolObj;
    16.             //
    17.             // Do stuff
    18.         }
    19. }
    20.