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 0mr_ashutosh0 · Nov 04, 2014 at 03:26 PM · transformparentchildparent-childparent transform

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 ?

alt text

untitled5.png (137.2 kB)
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 Owen-Reynolds · Nov 04, 2014 at 05:04 PM 0
Share

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?

avatar image 0mr_ashutosh0 · Nov 04, 2014 at 07:22 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 5 · 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 0mr_ashutosh0 · Nov 04, 2014 at 07:20 PM 0
Share

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.

avatar image spanagiotis · Nov 04, 2014 at 08:35 PM 0
Share

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.

avatar image Owen-Reynolds · Nov 05, 2014 at 12:50 AM 0
Share

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.

avatar image spanagiotis · Nov 05, 2014 at 12:54 AM 0
Share

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.

avatar image 0mr_ashutosh0 · Nov 05, 2014 at 01:46 PM 0
Share

@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.

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

28 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

Related Questions

Make a simple tree 1 Answer

Silly Instantiation Issue 0 Answers

Script that mimics a child object behavior. 1 Answer

Make child's transform independent of parent 1 Answer

Does transform.root work when a parent is made the child of another object through script? 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