- Home /
Parent object is getting generated why ??
using UnityEngine;
using System.Collections.Generic;
public class Pool : MonoBehaviour {
public GameObject RedS; // this and other three public objects below are not a problem
public GameObject CyanS;
public GameObject OrangeS;
public GameObject GreenS;
public GameObject[] objects;
public int[] number;
public List<GameObject>[] pool;
// Use this for initialization
void Start () {
Instantiate(RedS, new Vector3(-6.0f,-9.0f,-0.4f), Quaternion.identity);
Instantiate(CyanS, new Vector3(-2.0f,-9.0f,-0.4f), Quaternion.identity);
Instantiate(OrangeS, new Vector3(2.0f,-9.0f,-0.4f), Quaternion.identity);
Instantiate(GreenS, new Vector3(6.0f,-9.0f,-0.4f), Quaternion.identity);
instant();
}
void instant(){
pool = new List<GameObject>[objects.Length];
for(int count=0;count < objects.Length ; count++)
{
pool[count] = new List<GameObject>();
for(int num=0;num<number[count];num++)
{
GameObject temp = (GameObject)Instantiate(objects[count]);
temp.transform.parent = this.transform; // MAYBE HERE
pool[count].Add (temp);
}
}
}
public GameObject activate(int id, Vector3 position, Quaternion rotation)
{
for(int count = 0; count < pool[id].Count ; count++)
{
if(!pool[id][count].activeSelf)
{
pool[id][count].SetActive(true);
pool[id][count].transform.position = position;
pool[id][count].transform.rotation = rotation;
return pool[id][count];
}
}
return null;
}
public void deActivate(GameObject deActivateObject)
{
deActivateObject.SetActive(false);
}
}
Please look at this code Here as you see in the picture, i want only the children of pool to generate but at the start of game whole bunch of object pool gets generated which i dont want. What is wrong with this code ?
Does the picture show the problem? It looks fine to me. A single "pool" parent, with the script, with all the kids it made. What's the problem?
The only $$anonymous$$or thing I see is that they all start Active (but that isn't what you asked about.)
The poolees -- the things loaded into your object
array -- are Red, Cyan and maybe more we can't see. Right?
hello Owen The Problem is that all of the children of pool are getting 'ACTIVATED' ins$$anonymous$$d of 'INSTANTIATED'. They all START and get activated ins$$anonymous$$d of just initialized.
That single CUBE represents these 40 child object if you go in the editor and change their position they are overlapped and looking like one Cube.
Answer by spanagiotis · Nov 04, 2014 at 06:21 PM
The code looks fine, either you're not showing the entire problem in that screen shot or your title is misleading (unintentionally).
This script Pool
is a monobehavior, which means it needs to live on a GameObject
in the scene (and it seems to be on your 'pool' gameobject).
Inside instant()
you have this line of code:
temp.transform.parent = this.transform;
This line of code will take whatever game object you created (temp) and assign it as a child of whatever game object the script is attached to because you're assigning it to this.transform
.
If you don't want the children to be generated under the pool game object just remove that line. If you have a different issue that you're trying to solve, please edit your question to be clearer so we can help you.
EDIT: After re-reading your question multiple times. I'm wondering if you meant that you only want those 4 instances of red/cyan/etc to be instantiated. If that's the case, look at your Start()
method. You have the instant()
function at the end, which will be called and do all the children generation.
Remove the instant()
from Start()
and the pool won't have its children generated, but you'll be left with those four instances you want. Those are in the hierarchy as (Clone)
.
The Problem is that all of the children of pool are getting 'ACTIVATED' ins$$anonymous$$d of 'INSTANTIATED'.
the top items are not the problem
Problem is that as soon as i click play button in editor the pool object along with children get activated I have not written any code for them to get activated at the start they are meant to be activated only one touches the screen which is divided into 4 for these 4 color cubes.( above 4 cubes represented act as a gun and these pool objects are its projectile ).
That single CUBE represents these 40 child object. if you go in the editor and change their position they are overlapped and looking like one Cube.
Instantiate means making a copy. So if your object is active/enabled/etc, then that's what you're going to get. Look at the prefabs that make up red/cyan/etc inside your resources folder. Uncheck the box next to their names in the inspector. Now when they are instantiated they won't be active.
You could also do this:
temp.SetActive(false);
Place that line of code below the parenting line. This will deactivate the object.
Or, spanagiotis, note how there's an (unused) deActivate function at the bottom. I'm wondering if this is (partly?) borrowed code.
I think the problem is the OP is a little overwhelmed by it all, and is trying something a little too complex to learn parents + Instantiate + Active/Inactive + coding.
Right there is that method. I do agree, though. It seems the OP has a lack of understanding in basics. I'd suggest following one of the Unity beginner tutorials. Specifically This One. As it seems to include topics which you were requesting help with.
@owen Yes its a tutorial code from one of the youtube channel for Object Pooling. but the original code worked fine until i changed game objects. @spanagiotis thanks for the links. i will study that.